Skip to content

Commit c99f868

Browse files
Refactor folder3d to avoid complex-structure (C901) issue (#2185)
* Refactored-make_folder3d_dataset-ruff-error-C901 (#1926) Signed-off-by: sahusiddharth <[email protected]> * Simplify folder 3d dataset (#2184) --------- Signed-off-by: sahusiddharth <[email protected]> Co-authored-by: Siddharth Sahu <[email protected]>
1 parent b36b780 commit c99f868

File tree

1 file changed

+27
-33
lines changed

1 file changed

+27
-33
lines changed

src/anomalib/data/depth/folder_3d.py

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from anomalib.data.utils.path import _prepare_files_labels, validate_and_resolve_path
2525

2626

27-
def make_folder3d_dataset( # noqa: C901
27+
def make_folder3d_dataset(
2828
normal_dir: str | Path,
2929
root: str | Path | None = None,
3030
abnormal_dir: str | Path | None = None,
@@ -78,37 +78,28 @@ def make_folder3d_dataset( # noqa: C901
7878
msg = "A folder location must be provided in normal_dir."
7979
raise ValueError(msg)
8080

81-
filenames = []
82-
labels = []
83-
dirs = {DirType.NORMAL: normal_dir}
84-
85-
if abnormal_dir:
86-
dirs[DirType.ABNORMAL] = abnormal_dir
87-
88-
if normal_test_dir:
89-
dirs[DirType.NORMAL_TEST] = normal_test_dir
90-
91-
if normal_depth_dir:
92-
dirs[DirType.NORMAL_DEPTH] = normal_depth_dir
93-
94-
if abnormal_depth_dir:
95-
dirs[DirType.ABNORMAL_DEPTH] = abnormal_depth_dir
96-
97-
if normal_test_depth_dir:
98-
dirs[DirType.NORMAL_TEST_DEPTH] = normal_test_depth_dir
99-
100-
if mask_dir:
101-
dirs[DirType.MASK] = mask_dir
102-
103-
for dir_type, path in dirs.items():
104-
filename, label = _prepare_files_labels(path, dir_type, extensions)
105-
filenames += filename
106-
labels += label
81+
dirs = {
82+
DirType.NORMAL: normal_dir,
83+
DirType.ABNORMAL: abnormal_dir,
84+
DirType.NORMAL_TEST: normal_test_dir,
85+
DirType.NORMAL_DEPTH: normal_depth_dir,
86+
DirType.ABNORMAL_DEPTH: abnormal_depth_dir,
87+
DirType.NORMAL_TEST_DEPTH: normal_test_depth_dir,
88+
DirType.MASK: mask_dir,
89+
}
90+
91+
filenames: list[Path] = []
92+
labels: list[str] = []
93+
94+
for dir_type, dir_path in dirs.items():
95+
if dir_path is not None:
96+
filename, label = _prepare_files_labels(dir_path, dir_type, extensions)
97+
filenames += filename
98+
labels += label
10799

108100
samples = DataFrame({"image_path": filenames, "label": labels})
109101
samples = samples.sort_values(by="image_path", ignore_index=True)
110102

111-
# Create label index for normal (0) and abnormal (1) images.
112103
samples.loc[
113104
(samples.label == DirType.NORMAL) | (samples.label == DirType.NORMAL_TEST),
114105
"label_index",
@@ -137,9 +128,12 @@ def make_folder3d_dataset( # noqa: C901
137128
.all()
138129
)
139130
if not mismatch:
140-
msg = """Mismatch between anomalous images and depth images. Make sure the mask files
141-
in 'xyz' folder follow the same naming convention as the anomalous images in the dataset
142-
(e.g. image: '000.png', depth: '000.tiff')."""
131+
msg = (
132+
"Mismatch between anomalous images and depth images. "
133+
"Make sure the mask files in 'xyz' folder follow the same naming "
134+
"convention as the anomalous images in the dataset"
135+
"(e.g. image: '000.png', depth: '000.tiff')."
136+
)
143137
raise MisMatchError(msg)
144138

145139
missing_depth_files = samples.depth_path.apply(
@@ -159,7 +153,7 @@ def make_folder3d_dataset( # noqa: C901
159153
samples["mask_path"] = samples["mask_path"].fillna("")
160154
samples = samples.astype({"mask_path": "str"})
161155

162-
# make sure all the files exist
156+
# Make sure all the files exist
163157
if not samples.mask_path.apply(
164158
lambda x: Path(x).exists() if x != "" else True,
165159
).all():
@@ -168,7 +162,7 @@ def make_folder3d_dataset( # noqa: C901
168162
else:
169163
samples["mask_path"] = ""
170164

171-
# remove all the rows with temporal image samples that have already been assigned
165+
# Remove all the rows with temporal image samples that have already been assigned
172166
samples = samples.loc[
173167
(samples.label == DirType.NORMAL) | (samples.label == DirType.ABNORMAL) | (samples.label == DirType.NORMAL_TEST)
174168
]

0 commit comments

Comments
 (0)