Skip to content

Commit 112003e

Browse files
committed
writing pathology patches process functions
1 parent 7f847d1 commit 112003e

File tree

8 files changed

+41
-19
lines changed

8 files changed

+41
-19
lines changed

HyperG/utils/data/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .mri.read import read_mri
1+
from .mri.io import read_mri_series, save_mri_series
22
from .data_helper import split_id

HyperG/utils/data/mri/io.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os.path as osp
2+
3+
import SimpleITK as sitk
4+
import matplotlib.pyplot as plt
5+
import torch
6+
7+
8+
def read_mri_series(mri_path):
9+
img_itk = sitk.ReadImage(mri_path)
10+
img_np = sitk.GetArrayFromImage(img_itk)
11+
# SimpleITK read image as (z, y, x), need to be transposed to (x, y, z)
12+
img_np = img_np.transpose((2, 1, 0)).astype('float')
13+
14+
return torch.tensor(img_np).float()
15+
16+
17+
def save_mri_series(mris: torch.Tensor, save_dir, name_prefix):
18+
# mris should be image series with dimension (x, y, z)
19+
assert len(mris.shape) == 3
20+
# (x, y, z) -> (z, x, y)
21+
mris = mris.permute(2, 0, 1)
22+
23+
# save each mri into a specific direction
24+
for _idx in range(mris.size(0)):
25+
_mri = mris[_idx].squeeze().numpy()
26+
plt.imsave(osp.join(save_dir, f'{name_prefix}_{_idx}.jpg'), _mri, cmap=plt.cm.bone)

HyperG/utils/data/mri/read.py

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
def sample_patch():
2-
pass
1+
import os.path as osp
2+
3+
import openslide
4+
5+
6+
def sample_patches(slide_dir):
7+
slide = openslide.open_slide(slide_dir)
8+
slide_name = osp.basename(slide_dir)
9+
slide_name = slide_name[:slide_name.rfind('.')]
File renamed without changes.

examples/segment/heart_mri/data_helper.py renamed to examples/segmentation/heart_mri/data_helper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from HyperG.hyedge import gather_patch_ft, neighbor_grid, neighbor_distance
1010
from HyperG.hygraph import hyedge_concat
11-
from HyperG.utils.data import split_id, read_mri
11+
from HyperG.utils.data import split_id, read_mri_series
1212

1313

1414
def normalize(x):
@@ -86,10 +86,10 @@ def preprocess(data_list, patch_size, k_nearest):
8686

8787
def process_mri_seg(data, patch_size):
8888
# M x N x C -> C x M x N -> 1 x C x M x N
89-
img = read_mri(data['img']).permute(2, 0, 1).unsqueeze(0)
89+
img = read_mri_series(data['img']).permute(2, 0, 1).unsqueeze(0)
9090
row_num, col_num = img.size(2), img.size(3)
9191
# M x N x 1 -> M x N
92-
lbl = read_mri(data['seg']).squeeze()
92+
lbl = read_mri_series(data['seg']).squeeze()
9393

9494
# 1 x C x M x N -> 1 x Ckk x M x N -> M x N x Ckk
9595
img_patched = gather_patch_ft(img, patch_size).permute(2, 3, 1, 0).squeeze()
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import pytest
22

3-
from HyperG.utils.data import read_mri
3+
from HyperG.utils.data import read_mri_series
44

55

66
@pytest.mark.skip(reason='unpleasure')
77
def test_read_mri():
88
for _img_name in ['0001', '0002', '0003', '0004', '0005']:
99
img_dir = f'/repository/HyperG_example/example_data/heart_mri/processed/{_img_name}.mha'
1010
save_dir = f'/repository/HyperG_example/tmp/heart_mri/{_img_name}.jpg'
11-
img = read_mri(img_dir)
11+
img = read_mri_series(img_dir)
1212

1313
print(img.shape)
1414
import matplotlib.pyplot as plt

0 commit comments

Comments
 (0)