| 
 | 1 | +import numpy as np  | 
 | 2 | +import os  | 
 | 3 | + | 
 | 4 | +from wkcuber.cubing import ensure_wkw  | 
 | 5 | +from wkcuber.utils import WkwDatasetInfo, open_wkw  | 
 | 6 | +from wkcuber.metadata import (  | 
 | 7 | +    write_webknossos_metadata,  | 
 | 8 | +    refresh_metadata,  | 
 | 9 | +    read_datasource_properties,  | 
 | 10 | +    read_metadata_for_layer,  | 
 | 11 | +)  | 
 | 12 | + | 
 | 13 | + | 
 | 14 | +def test_element_class_convertion():  | 
 | 15 | +    test_wkw_path = os.path.join("testoutput", "test_metadata")  | 
 | 16 | +    prediction_layer_name = "prediction"  | 
 | 17 | +    prediction_wkw_info = WkwDatasetInfo(  | 
 | 18 | +        test_wkw_path, prediction_layer_name, np.float32, 1  | 
 | 19 | +    )  | 
 | 20 | +    ensure_wkw(prediction_wkw_info, num_channels=3)  | 
 | 21 | + | 
 | 22 | +    write_custom_layer(test_wkw_path, "prediction", np.float32, num_channels=3)  | 
 | 23 | +    write_webknossos_metadata(  | 
 | 24 | +        test_wkw_path,  | 
 | 25 | +        "test_metadata",  | 
 | 26 | +        [11.24, 11.24, 28],  | 
 | 27 | +        compute_max_id=True,  | 
 | 28 | +        exact_bounding_box={"topLeft": [0, 0, 0], "width": 4, "height": 4, "depth": 4},  | 
 | 29 | +    )  | 
 | 30 | +    write_custom_layer(test_wkw_path, "segmentation", np.float64, num_channels=1)  | 
 | 31 | +    write_custom_layer(test_wkw_path, "color", np.uint8, num_channels=3)  | 
 | 32 | + | 
 | 33 | +    refresh_metadata(test_wkw_path)  | 
 | 34 | + | 
 | 35 | +    check_element_class_of_layer(test_wkw_path, "prediction", "float", np.float32)  | 
 | 36 | +    check_element_class_of_layer(test_wkw_path, "segmentation", "double", np.float64)  | 
 | 37 | +    check_element_class_of_layer(test_wkw_path, "color", "uint24", np.uint8)  | 
 | 38 | + | 
 | 39 | + | 
 | 40 | +def check_element_class_of_layer(  | 
 | 41 | +    test_wkw_path, layer_name, expected_element_class, expected_dtype  | 
 | 42 | +):  | 
 | 43 | +    datasource_properties = read_datasource_properties(test_wkw_path)  | 
 | 44 | +    layer_to_check = None  | 
 | 45 | +    for layer in datasource_properties["dataLayers"]:  | 
 | 46 | +        if layer["name"] == layer_name:  | 
 | 47 | +            layer_to_check = layer  | 
 | 48 | + | 
 | 49 | +    assert (  | 
 | 50 | +        layer_to_check  | 
 | 51 | +    ), f"Did not find layer {layer_name} in datasource_properties.json."  | 
 | 52 | +    assert layer_to_check["elementClass"] == expected_element_class  | 
 | 53 | +    _, converted_dtype, _, _ = read_metadata_for_layer(test_wkw_path, layer_name)  | 
 | 54 | +    assert converted_dtype == expected_dtype  | 
 | 55 | + | 
 | 56 | + | 
 | 57 | +def write_custom_layer(target_path, layer_name, dtype, num_channels):  | 
 | 58 | +    data = (  | 
 | 59 | +        np.arange(4 * 4 * 4 * num_channels)  | 
 | 60 | +        .reshape((num_channels, 4, 4, 4))  | 
 | 61 | +        .astype(dtype)  | 
 | 62 | +    )  | 
 | 63 | +    prediction_wkw_info = WkwDatasetInfo(target_path, layer_name, dtype, 1)  | 
 | 64 | +    ensure_wkw(prediction_wkw_info, num_channels=num_channels)  | 
 | 65 | +    with open_wkw(prediction_wkw_info, num_channels=num_channels) as dataset:  | 
 | 66 | +        dataset.write(off=(0, 0, 0), data=data)  | 
 | 67 | + | 
 | 68 | + | 
 | 69 | +if __name__ == "__main__":  | 
 | 70 | +    test_element_class_convertion()  | 
0 commit comments