Skip to content

Commit

Permalink
fixes and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ainur Karimov committed Dec 19, 2024
1 parent 4705c0b commit a4bce08
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ train:
target: ./data/stroke/train
infer:
source: https://api.blackhole.ai.innopolis.university/public-datasets/rtk/complex_infer.zip
target: /data/complex/infer
target: ./data/complex/infer
2 changes: 1 addition & 1 deletion examples/metrics_ct_brain_hemorrhage_detection.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if [ -z "$data_path" ]; then
fi

if [ -z "$output" ]; then
output="../innofw/logs/infer/segmentation/semantic-segmentation/SK_180822_qmciwj41_unet_brain_rtk/"
output="../innofw/logs/infer/segmentation/semantic-segmentation/AK_081224_gwVOeQ_unet_brain_rtk/"
output+="$(ls $output | tail -n 1)"
echo "Using default output path $output"
fi
Expand Down
2 changes: 1 addition & 1 deletion examples/metrics_ct_brain_hemorrhage_segmentation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ if [ -z "$data_path" ]; then
fi

if [ -z "$output" ]; then
output="../innofw/logs/infer/segmentation/semantic-segmentation/SK_180822_qmciwj41_unet_brain_rtk/"
output="../innofw/logs/infer/segmentation/semantic-segmentation/AK_081224_gwVOeQ_unet_brain_rtk/"
output+="$(ls $output -tr| tail -n 1)"
echo "Using default output path $output"
fi
Expand Down
2 changes: 1 addition & 1 deletion examples/metrics_ct_mri_complexing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if [ -z "$data_path" ]; then
fi

if [ -z "$output" ]; then
output="../innofw/logs/infer/segmentation/semantic-segmentation/SK_100923_unet_brain_complex.yaml/"
output="../innofw/logs/infer/segmentation/semantic-segmentation/AK_081224_Yjc97FX_unet_brain_complex.yaml/"
output+="$(ls $output -tr | tail -n 1)"
fi
python innofw/utils/data_utils/rtk/CT_complexing_metrics.py -i $data_path -o $output
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def hemorrhage_contrast_metrics(input_path: str):
metrics = calculate_metrics(raw_image, image)
plt.suptitle("\n".join([f"{k}:{np.round(v, 2)}" for k, v in metrics.items()]))
plt.show()
plt.close("all")


def callback(arguments):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def hemorrhage_contrast(input_path: str, output_folder: str = None):
except TypeError:
raise ValueError(f"Wrong path to save: {output_folder}")
if urlparse(input_path):
default_path = "innofw/data/rtk/infer/"
default_path = "./data/rtk/infer/"
path = {"source": input_path, "target": default_path}
else:
path = {"source": input_path, "target": input_path}
Expand Down
8 changes: 3 additions & 5 deletions innofw/utils/data_utils/rtk/CT_hemorrhage_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def compute_metrics(gt_boxes, pr_boxes, iou_threshold=0.5):
return metrics


def processing(input_path, output_folder, task="detection"):
def process_metrics(input_path, output_folder, task="detection"):

dataset = DicomCocoDatasetRTK(data_dir=input_path, transform=transform)
outs = os.listdir(output_folder)
Expand Down Expand Up @@ -107,6 +107,7 @@ def processing(input_path, output_folder, task="detection"):
patch = Patch(facecolor="red", edgecolor="r", label="pathology")
f.legend(handles=[patch], loc="lower center")
plt.show()
plt.close("all")


def result_bbox(masks, image):
Expand Down Expand Up @@ -168,15 +169,12 @@ def draw_bboxes(image, bboxes):
Returns:
np.ndarray: Изображение с отрисованными bounding boxes.
"""

color = [255, 0, 0]

for bbox in bboxes:
if not bbox:
break

x_min, y_min, x_max, y_max = bbox

cv2.rectangle(image, (x_min, y_min), (x_max, y_max), color, 2)

return image
Expand All @@ -185,7 +183,7 @@ def draw_bboxes(image, bboxes):
def callback(arguments):
"""Callback function for arguments"""
try:
processing(arguments.input, arguments.output, arguments.task)
process_metrics(arguments.input, arguments.output, arguments.task)
except KeyboardInterrupt:
print("You exited")

Expand Down
36 changes: 33 additions & 3 deletions tests/unit/datamodules/lightning_datamodules/test_rtk.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest.mock import patch
import os
import shutil

Expand All @@ -14,8 +15,10 @@
)
from innofw.core.datasets.coco_rtk import DicomCocoDatasetRTK
from innofw.utils.data_utils.preprocessing.CT_hemorrhage_contrast_rtk import (
hemorrhage_contrast,
hemorrhage_contrast, transform as resize_transform
)
from innofw.utils.data_utils.rtk.CT_hemorrhage_metrics import process_metrics
from innofw.utils.data_utils.preprocessing.CT_hemorrhage_contrast_metrics import hemorrhage_contrast_metrics

rtk_complex = "https://api.blackhole.ai.innopolis.university/public-datasets/rtk/complex_infer.zip"
rtk_segm = "https://api.blackhole.ai.innopolis.university/public-datasets/rtk/infer.zip"
Expand Down Expand Up @@ -47,7 +50,7 @@ def test_DicomCocoDataModuleRTK(transform):
shutil.rmtree(target_dir)

path = {"source": rtk_segm, "target": target_dir}
dm = DicomCocoDataModuleRTK(infer=path, transform=transform)
dm = DicomCocoDataModuleRTK(infer=path, transform=resize_transform)
dm.setup_infer()
ds = dm.predict_dataloader()
for batch in ds:
Expand Down Expand Up @@ -97,7 +100,8 @@ def test_datamodule_description():
assert key in ds


def test_hemor_contrast(tmp_path_factory):
@patch("matplotlib.pyplot.show")
def test_hemor_contrast(mock_show, tmp_path_factory):
target_dir = "./data/rtk/infer"
if os.path.exists(target_dir):
shutil.rmtree(target_dir)
Expand All @@ -108,3 +112,29 @@ def test_hemor_contrast(tmp_path_factory):
assert len(content) % 3 == 0
assert np.any([x.endswith("npy") for x in content])
assert np.any([x.endswith("png") for x in content])

hemorrhage_contrast_metrics(out_)
assert mock_show.call_count > 0


@pytest.mark.parametrize("task", ["segmentation", "detection"])
@patch("matplotlib.pyplot.show")
def test_segm_detection_pipeline_metrics(mock_show, tmp_path_factory, task):

# just to imitate data loading
target_dir = tmp_path_factory.mktemp("target_dir")
path = {"source": rtk_segm, "target": target_dir}
dm = DicomCocoDataModuleRTK(infer=path, transform=resize_transform)
dm.setup_infer()
ds = dm.predict_dataloader()
ds.transform = resize_transform

samples_number = len(ds)

out_dir = tmp_path_factory.mktemp("out")
for i in range(samples_number):
random_numpy = np.random.randint(0, 1, [256,256,1])
np.save(os.path.join(out_dir, f"{i}.npy"), random_numpy)

process_metrics(input_path=target_dir, output_folder=out_dir)
assert mock_show.call_count > 0

0 comments on commit a4bce08

Please sign in to comment.