simcats-datasets 2.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simcats_datasets/__init__.py +2 -0
- simcats_datasets/generation/__init__.py +6 -0
- simcats_datasets/generation/_create_dataset.py +221 -0
- simcats_datasets/generation/_create_simulated_dataset.py +372 -0
- simcats_datasets/loading/__init__.py +8 -0
- simcats_datasets/loading/_load_dataset.py +177 -0
- simcats_datasets/loading/load_ground_truth.py +486 -0
- simcats_datasets/loading/pytorch.py +426 -0
- simcats_datasets/support_functions/__init__.py +1 -0
- simcats_datasets/support_functions/_json_encoders.py +51 -0
- simcats_datasets/support_functions/clip_line_to_rectangle.py +191 -0
- simcats_datasets/support_functions/convert_lines.py +110 -0
- simcats_datasets/support_functions/data_preprocessing.py +351 -0
- simcats_datasets/support_functions/get_lead_transition_labels.py +102 -0
- simcats_datasets/support_functions/pytorch_format_output.py +170 -0
- simcats_datasets-2.4.0.dist-info/LICENSE +674 -0
- simcats_datasets-2.4.0.dist-info/METADATA +837 -0
- simcats_datasets-2.4.0.dist-info/RECORD +20 -0
- simcats_datasets-2.4.0.dist-info/WHEEL +5 -0
- simcats_datasets-2.4.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"""Functions for formatting the output of the **Pytorch Dataset class**.
|
|
2
|
+
|
|
3
|
+
Every function must accept a CSD (as array), a ground truth (e.g. TCT mask as array) and the image id as input.
|
|
4
|
+
Output type depends on the ground truth type and the required pytorch datatype (tensor as long, float, ...). Ground
|
|
5
|
+
truth could for example be a pixel mask or defined start end points of lines.
|
|
6
|
+
**Please look at format_dict_csd_float_ground_truth_long for a reference.**
|
|
7
|
+
|
|
8
|
+
@author: f.hader
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
from typing import Tuple
|
|
14
|
+
|
|
15
|
+
import numpy as np
|
|
16
|
+
import torch
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def format_dict_csd_float_ground_truth_long(csd: np.ndarray, ground_truth: np.ndarray, idx: int, ) -> dict[
|
|
20
|
+
str, torch.Tensor]:
|
|
21
|
+
"""Format the output of the Pytorch Dataset class to be a dict with entries 'csd' and 'ground_truth' of dtype float and long, respectively. (default of Pytorch Dataset class.)
|
|
22
|
+
|
|
23
|
+
Args:
|
|
24
|
+
csd: The CSD array.
|
|
25
|
+
ground_truth: Ground truth as pixel mask.
|
|
26
|
+
idx: index of the csd. Not used in this format.
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
Dict with 'csd' and 'ground_truth' of dtype float and long, respectively.
|
|
30
|
+
"""
|
|
31
|
+
assert (
|
|
32
|
+
csd.size == ground_truth.size), f"Image and mask should be the same size, but are {csd.size=} and {ground_truth.size=}"
|
|
33
|
+
return {"csd": torch.as_tensor(csd.copy(), dtype=torch.float).contiguous(),
|
|
34
|
+
"ground_truth": torch.as_tensor(ground_truth.copy(), dtype=torch.long, ).contiguous(), }
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def format_dict_csd_float16_ground_truth_long(csd: np.ndarray, ground_truth: np.ndarray, idx: int, ) -> dict[
|
|
38
|
+
str, torch.Tensor]:
|
|
39
|
+
"""Format the output of the Pytorch Dataset class to be a dict with entries 'csd' and 'ground_truth' of dtype float16 and long, respectively.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
csd: The CSD array.
|
|
43
|
+
ground_truth: Ground truth as pixel mask.
|
|
44
|
+
idx: index of the csd. Not used in this format.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
Dict with 'csd' and 'ground_truth' of dtype float16 and long, respectively.
|
|
48
|
+
"""
|
|
49
|
+
assert (
|
|
50
|
+
csd.size == ground_truth.size), f"Image and mask should be the same size, but are {csd.size=} and {ground_truth.size=}"
|
|
51
|
+
return {"csd": torch.as_tensor(csd.copy(), dtype=torch.float16).contiguous(),
|
|
52
|
+
"ground_truth": torch.as_tensor(ground_truth.copy(), dtype=torch.long, ).contiguous(), }
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def format_dict_csd_float_ground_truth_float(csd: np.ndarray, ground_truth: np.ndarray, idx: int, ) -> dict[
|
|
56
|
+
str, torch.Tensor]:
|
|
57
|
+
"""Format the output of the Pytorch Dataset class to be a dict with entries 'csd' and 'ground_truth' of dtype float and float, respectively.
|
|
58
|
+
|
|
59
|
+
Args:
|
|
60
|
+
csd: The CSD array.
|
|
61
|
+
ground_truth: Ground truth as pixel mask.
|
|
62
|
+
idx: index of the csd. Not used in this format.
|
|
63
|
+
|
|
64
|
+
Returns:
|
|
65
|
+
Dict with 'csd' and 'ground_truth' of dtype float and float, respectively.
|
|
66
|
+
"""
|
|
67
|
+
assert (
|
|
68
|
+
csd.size == ground_truth.size), f"Image and mask should be the same size, but are {csd.size=} and {ground_truth.size=}"
|
|
69
|
+
return {"csd": torch.as_tensor(csd.copy(), dtype=torch.float).contiguous(),
|
|
70
|
+
"ground_truth": torch.as_tensor(ground_truth.copy(), dtype=torch.float).contiguous(), }
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def format_mmsegmentation(csd: np.ndarray, ground_truth: np.ndarray, idx: int, ) -> dict[str, torch.Tensor]:
|
|
74
|
+
"""Format the output of the Pytorch Dataset class to be conform to the MMSegmentation CustomDataset of version 0.6.0, see https://github.com/open-mmlab/mmsegmentation/blob/v0.6.0/mmseg/datasets/custom.py.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
csd: The CSD array.
|
|
78
|
+
ground_truth: Ground truth as pixel mask.
|
|
79
|
+
idx: index of the csd.
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
Dict with data conform to the MMSegmentation CustomDataset of version 0.6.0, see https://github.com/open-mmlab/mmsegmentation/blob/v0.6.0/mmseg/datasets/custom.py.
|
|
83
|
+
"""
|
|
84
|
+
assert (
|
|
85
|
+
csd.size == ground_truth.size), f"Image and mask should be the same size, but are {csd.size=} and {ground_truth.size=}"
|
|
86
|
+
return {"img": torch.as_tensor(csd.copy()).float().contiguous(),
|
|
87
|
+
"gt_semantic_seg": torch.as_tensor(ground_truth.copy()).float().contiguous(),
|
|
88
|
+
"img_metas": {"filename": f"{idx}.jpg", "ori_filename": f"{idx}_ori.jpg", "ori_shape": csd.shape[::-1],
|
|
89
|
+
# we want (100, 100, 1) not (1, 100, 100)
|
|
90
|
+
"img_shape": csd.shape[::-1], "pad_shape": csd.shape[::-1], # image shape after padding
|
|
91
|
+
"scale_factor": 1.0, "img_norm_cfg": {"mean": np.mean(csd, axis=(-2, -1)), # mean for each channel
|
|
92
|
+
"std": np.std(csd, axis=(-2, -1)), # std for each channel
|
|
93
|
+
"to_rgb": False, }, "img_id": f"{idx}", }, }
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def format_csd_only(csd: np.ndarray, ground_truth: np.ndarray, idx: int, ) -> torch.Tensor:
|
|
97
|
+
"""Format the output of the Pytorch Dataset class to be just a CSD.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
csd: The CSD array.
|
|
101
|
+
ground_truth: Ground truth as pixel mask. Not used in this format.
|
|
102
|
+
idx: Index of the csd. Not used in this format.
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
The CSD as tensor.
|
|
106
|
+
"""
|
|
107
|
+
return torch.as_tensor(csd.copy(), dtype=torch.float).contiguous()
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def format_csd_float16_only(csd: np.ndarray, ground_truth: np.ndarray, idx: int, ) -> torch.Tensor:
|
|
111
|
+
"""Format the output of the Pytorch Dataset class to be just a float16 (half precision) CSD.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
csd: The CSD array.
|
|
115
|
+
ground_truth: Ground truth as pixel mask. Not used in this format.
|
|
116
|
+
idx: Index of the csd. Not used in this format.
|
|
117
|
+
|
|
118
|
+
Returns:
|
|
119
|
+
The float 16 (half precision) CSD as tensor.
|
|
120
|
+
"""
|
|
121
|
+
return torch.as_tensor(csd.copy(), dtype=torch.float16).contiguous()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def format_csd_bfloat16_only(csd: np.ndarray, ground_truth: np.ndarray, idx: int, ) -> torch.Tensor:
|
|
125
|
+
"""Format the output of the Pytorch Dataset class to be just a bfloat16 (half precision) CSD.
|
|
126
|
+
|
|
127
|
+
Args:
|
|
128
|
+
csd: The CSD array.
|
|
129
|
+
ground_truth: Ground truth as pixel mask. Not used in this format.
|
|
130
|
+
idx: Index of the csd. Not used in this format.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
The brain float 16 (half precision) CSD as tensor.
|
|
134
|
+
"""
|
|
135
|
+
return torch.as_tensor(csd.copy(), dtype=torch.bfloat16).contiguous()
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def format_csd_class_index(csd: np.ndarray, ground_truth: np.ndarray, idx: int, ) -> Tuple[
|
|
139
|
+
torch.Tensor, torch.Tensor, int]:
|
|
140
|
+
"""Format the output of the Pytorch Dataset class to be the CSD, a class index (which is always 0 as we have no classes) and the index.
|
|
141
|
+
|
|
142
|
+
This is needed to be conform to the datasets used in DeepSVDD, see https://github.com/lukasruff/Deep-SVDD-PyTorch.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
csd: The CSD array.
|
|
146
|
+
ground_truth: Ground truth as pixel mask. Not used in this format.
|
|
147
|
+
idx: Index of the csd.
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
A tuple of CSD, class index, and the index.
|
|
151
|
+
"""
|
|
152
|
+
return torch.as_tensor(csd.copy(), dtype=torch.float).unsqueeze(0).contiguous(), torch.tensor(0), idx
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def format_tuple_csd_float_ground_truth_float(csd: np.ndarray, ground_truth: np.ndarray, idx: int, ) -> dict[
|
|
156
|
+
str, torch.Tensor]:
|
|
157
|
+
"""Format the output of the Pytorch Dataset class to be a tuple of the csd and the ground_truth.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
csd: The CSD array.
|
|
161
|
+
ground_truth: Ground truth as pixel mask.
|
|
162
|
+
idx: index of the csd. Not used in this format.
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
Tuple with csd and ground_truth of dtype float and float, respectively.
|
|
166
|
+
"""
|
|
167
|
+
assert (
|
|
168
|
+
csd.size == ground_truth.size), f"Image and mask should be the same size, but are {csd.size=} and {ground_truth.size=}"
|
|
169
|
+
return (torch.as_tensor(csd.copy(), dtype=torch.float).contiguous(),
|
|
170
|
+
torch.as_tensor(ground_truth.copy(), dtype=torch.float).contiguous(),)
|