|
| 1 | +from n2v.internals.N2V_DataGenerator import N2V_DataGenerator |
| 2 | +import urllib.request |
| 3 | +import os |
| 4 | +import zipfile |
| 5 | + |
| 6 | + |
| 7 | +def test_generate_patches_2D(): |
| 8 | + |
| 9 | + if not os.path.isdir('data'): |
| 10 | + os.mkdir('data') |
| 11 | + zip_path = "data/RGB.zip" |
| 12 | + if not os.path.exists(zip_path): |
| 13 | + data = urllib.request.urlretrieve('https://cloud.mpi-cbg.de/index.php/s/Frru2hsjjAljpfW/download', zip_path) |
| 14 | + with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 15 | + zip_ref.extractall('data') |
| 16 | + |
| 17 | + datagen = N2V_DataGenerator() |
| 18 | + |
| 19 | + imgs = datagen.load_imgs_from_directory(directory="data", filter='*.png', dims='YXC') |
| 20 | + imgs[0] = imgs[0][..., :3] |
| 21 | + patches = datagen.generate_patches_from_list(imgs, shape=(1100, 2800)) |
| 22 | + assert len(patches) == 1 |
| 23 | + patches = datagen.generate_patches_from_list(imgs, shape=(550, 1400)) |
| 24 | + assert len(patches) == 4 |
| 25 | + patches = datagen.generate_patches_from_list(imgs, shape=(110, 280)) |
| 26 | + assert len(patches) == 100 |
| 27 | + |
| 28 | +def test_generate_patches_3D(): |
| 29 | + |
| 30 | + if not os.path.isdir('data'): |
| 31 | + os.mkdir('data') |
| 32 | + zip_path = 'data/flywing-data.zip' |
| 33 | + if not os.path.exists(zip_path): |
| 34 | + # download and unzip data |
| 35 | + data = urllib.request.urlretrieve('https://cloud.mpi-cbg.de/index.php/s/RKStdwKo4FlFrxE/download', zip_path) |
| 36 | + with zipfile.ZipFile(zip_path, 'r') as zip_ref: |
| 37 | + zip_ref.extractall('data') |
| 38 | + |
| 39 | + datagen = N2V_DataGenerator() |
| 40 | + |
| 41 | + imgs = datagen.load_imgs_from_directory(directory="data", filter='*.tif', dims='ZYX') |
| 42 | + print(imgs[0].shape) |
| 43 | + patches = datagen.generate_patches_from_list(imgs[:1], shape=(35, 520, 692)) |
| 44 | + assert len(patches) == 1 |
| 45 | + patches = datagen.generate_patches_from_list(imgs[:1], shape=(5, 52, 174)) |
| 46 | + assert len(patches) == 210 |
| 47 | + |
0 commit comments