11import filecmp
22import json
33import os
4- from os .path import dirname
4+ from os .path import dirname , join
55import pytest
66
77import numpy as np
@@ -147,7 +147,7 @@ def test_create_wk_dataset_with_explicit_header_fields():
147147 delete_dir ("./testoutput/wk_dataset_advanced" )
148148
149149 ds = WKDataset .create ("./testoutput/wk_dataset_advanced" , scale = (1 , 1 , 1 ))
150- ds .add_layer ("color" , "color" , dtype_per_layer = "uint48" , num_channels = 3 )
150+ ds .add_layer ("color" , Layer . COLOR_TYPE , dtype_per_layer = "uint48" , num_channels = 3 )
151151
152152 ds .get_layer ("color" ).add_mag ("1" , block_len = 64 , file_len = 64 )
153153 ds .get_layer ("color" ).add_mag ("2-2-1" )
@@ -696,7 +696,9 @@ def test_tiled_tiff_read_and_write_multichannel():
696696 pattern = "{xxx}_{yyy}_{zzz}.tif" ,
697697 )
698698
699- mag = tiled_tiff_ds .add_layer ("color" , "color" , num_channels = 3 ).add_mag ("1" )
699+ mag = tiled_tiff_ds .add_layer ("color" , Layer .COLOR_TYPE , num_channels = 3 ).add_mag (
700+ "1"
701+ )
700702
701703 data = get_multichanneled_data (np .uint8 )
702704
@@ -715,7 +717,7 @@ def test_tiled_tiff_read_and_write():
715717 pattern = "{xxx}_{yyy}_{zzz}.tif" ,
716718 )
717719
718- mag = tiled_tiff_ds .add_layer ("color" , "color" ).add_mag ("1" )
720+ mag = tiled_tiff_ds .add_layer ("color" , Layer . COLOR_TYPE ).add_mag ("1" )
719721
720722 data = np .zeros ((250 , 200 , 10 ), dtype = np .uint8 )
721723 for h in range (10 ):
@@ -812,11 +814,11 @@ def test_largest_segment_id_requirement():
812814 ds = WKDataset .create (path , scale = (10 , 10 , 10 ))
813815
814816 with pytest .raises (AssertionError ):
815- ds .add_layer ("segmentation" , "segmentation" )
817+ ds .add_layer ("segmentation" , Layer . SEGMENTATION_TYPE )
816818
817819 largest_segment_id = 10
818820 ds .add_layer (
819- "segmentation" , "segmentation" , largest_segment_id = largest_segment_id
821+ "segmentation" , Layer . SEGMENTATION_TYPE , largest_segment_id = largest_segment_id
820822 ).add_mag (Mag (1 ))
821823
822824 ds = WKDataset (path )
@@ -1018,7 +1020,7 @@ def test_view_write_without_open():
10181020 delete_dir ("./testoutput/wk_dataset_write_without_open" )
10191021
10201022 ds = WKDataset .create ("./testoutput/wk_dataset_write_without_open" , scale = (1 , 1 , 1 ))
1021- ds .add_layer ("color" , "color" )
1023+ ds .add_layer ("color" , Layer . COLOR_TYPE )
10221024
10231025 ds .get_layer ("color" ).add_mag ("1" )
10241026
@@ -1214,7 +1216,7 @@ def test_view_offsets():
12141216 delete_dir ("./testoutput/wk_offset_tests" )
12151217
12161218 ds = WKDataset .create ("./testoutput/wk_offset_tests" , scale = (1 , 1 , 1 ))
1217- mag = ds .add_layer ("color" , "color" ).add_mag ("1" )
1219+ mag = ds .add_layer ("color" , Layer . COLOR_TYPE ).add_mag ("1" )
12181220
12191221 # The dataset is new -> no data has been written.
12201222 # Therefore, the size of the bounding box in the properties.json is (0, 0, 0)
@@ -1288,12 +1290,14 @@ def test_adding_layer_with_invalid_dtype_per_layer():
12881290 ds = WKDataset .create ("./testoutput/invalid_dtype" , scale = (1 , 1 , 1 ))
12891291 with pytest .raises (TypeError ):
12901292 # this would lead to a dtype_per_channel of "uint10", but that is not a valid dtype
1291- ds .add_layer ("color" , "color" , dtype_per_layer = "uint30" , num_channels = 3 )
1293+ ds .add_layer (
1294+ "color" , Layer .COLOR_TYPE , dtype_per_layer = "uint30" , num_channels = 3
1295+ )
12921296 with pytest .raises (TypeError ):
12931297 # "int" is interpreted as "int64", but 64 bit cannot be split into 3 channels
1294- ds .add_layer ("color" , "color" , dtype_per_layer = "int" , num_channels = 3 )
1298+ ds .add_layer ("color" , Layer . COLOR_TYPE , dtype_per_layer = "int" , num_channels = 3 )
12951299 ds .add_layer (
1296- "color" , "color" , dtype_per_layer = "int" , num_channels = 4
1300+ "color" , Layer . COLOR_TYPE , dtype_per_layer = "int" , num_channels = 4
12971301 ) # "int"/"int64" works with 4 channels
12981302
12991303
@@ -1485,3 +1489,39 @@ def test_add_symlink_layer():
14851489
14861490 assert np .array_equal (mag .read (size = (10 , 10 , 10 )), write_data )
14871491 assert np .array_equal (original_mag .read (size = (10 , 10 , 10 )), write_data )
1492+
1493+
1494+ def test_search_dataset_also_for_long_layer_name ():
1495+ delete_dir ("./testoutput/long_layer_name" )
1496+
1497+ ds = WKDataset .create ("./testoutput/long_layer_name" , scale = (1 , 1 , 1 ))
1498+ mag = ds .add_layer ("color" , Layer .COLOR_TYPE ).add_mag ("2" )
1499+
1500+ assert mag .name == "2"
1501+ short_mag_file_path = join (ds .path , "color" , Mag (mag .name ).to_layer_name ())
1502+ long_mag_file_path = join (ds .path , "color" , Mag (mag .name ).to_long_layer_name ())
1503+
1504+ assert os .path .exists (short_mag_file_path )
1505+ assert not os .path .exists (long_mag_file_path )
1506+
1507+ write_data = (np .random .rand (10 , 10 , 10 ) * 255 ).astype (np .uint8 )
1508+ mag .write (write_data , offset = (10 , 10 , 10 ))
1509+
1510+ assert np .array_equal (
1511+ mag .read (offset = (10 , 10 , 10 ), size = (10 , 10 , 10 )), np .expand_dims (write_data , 0 )
1512+ )
1513+
1514+ # rename the path from "long_layer_name/color/2" to "long_layer_name/color/2-2-2"
1515+ os .rename (short_mag_file_path , long_mag_file_path )
1516+
1517+ with pytest .raises (WKWException ):
1518+ # the dataset has to be reopened to notice the changed directory
1519+ mag .read (offset = (10 , 10 , 10 ), size = (10 , 10 , 10 ))
1520+
1521+ # when opening the dataset, it searches both for the long and the short path
1522+ layer = WKDataset ("./testoutput/long_layer_name" ).get_layer ("color" )
1523+ mag = layer .get_mag ("2" )
1524+ assert np .array_equal (
1525+ mag .read (offset = (10 , 10 , 10 ), size = (10 , 10 , 10 )), np .expand_dims (write_data , 0 )
1526+ )
1527+ layer .delete_mag ("2" )
0 commit comments