|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | + |
| 5 | +from fractal.tasks.lib_regions_of_interest import convert_FOV_ROIs_3D_to_2D |
| 6 | +from fractal.tasks.lib_regions_of_interest import convert_ROI_table_to_indices |
| 7 | +from fractal.tasks.lib_regions_of_interest import prepare_FOV_ROI_table |
| 8 | +from fractal.tasks.lib_regions_of_interest import ( |
| 9 | + split_3D_indices_into_z_layers, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +PIXEL_SIZE_X = 0.1625 |
| 14 | +PIXEL_SIZE_Y = 0.1625 |
| 15 | +PIXEL_SIZE_Z = 1.0 |
| 16 | + |
| 17 | +IMG_SIZE_X = 2560 |
| 18 | +IMG_SIZE_Y = 2160 |
| 19 | +NUM_Z_PLANES = 4 |
| 20 | + |
| 21 | + |
| 22 | +def get_metadata_dataframe(): |
| 23 | + """ |
| 24 | + Create artificial metadata dataframe |
| 25 | + """ |
| 26 | + df = pd.DataFrame(np.zeros((4, 10)), dtype=int) |
| 27 | + df.index = ["FOV1", "FOV2", "FOV3", "FOV4"] |
| 28 | + df.columns = [ |
| 29 | + "x_micrometer", |
| 30 | + "y_micrometer", |
| 31 | + "z_micrometer", |
| 32 | + "x_pixel", |
| 33 | + "y_pixel", |
| 34 | + "z_pixel", |
| 35 | + "pixel_size_x", |
| 36 | + "pixel_size_y", |
| 37 | + "pixel_size_z", |
| 38 | + "bit_depth", |
| 39 | + ] |
| 40 | + img_size_x_micrometer = IMG_SIZE_X * PIXEL_SIZE_X |
| 41 | + img_size_y_micrometer = IMG_SIZE_Y * PIXEL_SIZE_Y |
| 42 | + df["x_micrometer"] = [ |
| 43 | + 0.0, |
| 44 | + img_size_x_micrometer, |
| 45 | + 0.0, |
| 46 | + img_size_x_micrometer, |
| 47 | + ] |
| 48 | + df["y_micrometer"] = [ |
| 49 | + 0.0, |
| 50 | + 0.0, |
| 51 | + img_size_y_micrometer, |
| 52 | + img_size_y_micrometer, |
| 53 | + ] |
| 54 | + df["z_micrometer"] = [0.0, 0.0, 0.0, 0.0] |
| 55 | + df["x_pixel"] = [IMG_SIZE_X] * 4 |
| 56 | + df["y_pixel"] = [IMG_SIZE_Y] * 4 |
| 57 | + df["z_pixel"] = [NUM_Z_PLANES] * 4 |
| 58 | + df["pixel_size_x"] = [PIXEL_SIZE_X] * 4 |
| 59 | + df["pixel_size_y"] = [PIXEL_SIZE_Y] * 4 |
| 60 | + df["pixel_size_z"] = [PIXEL_SIZE_Z] * 4 |
| 61 | + df["bit_depth"] = [16.0] * 4 |
| 62 | + |
| 63 | + return df |
| 64 | + |
| 65 | + |
| 66 | +list_params = [ |
| 67 | + (0, 2), |
| 68 | + (1, 2), |
| 69 | + (2, 2), |
| 70 | + (3, 2), |
| 71 | + (0, 3), |
| 72 | + (1, 3), |
| 73 | + (2, 3), |
| 74 | + (3, 3), |
| 75 | + (0, 7), |
| 76 | + (1, 7), |
| 77 | + (2, 7), |
| 78 | +] |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("level,coarsening_xy", list_params) |
| 82 | +def test_ROI_indices_3D(level, coarsening_xy): |
| 83 | + |
| 84 | + metadata_dataframe = get_metadata_dataframe() |
| 85 | + adata = prepare_FOV_ROI_table(metadata_dataframe) |
| 86 | + |
| 87 | + full_res_pxl_sizes_zyx = [PIXEL_SIZE_Z, PIXEL_SIZE_Y, PIXEL_SIZE_X] |
| 88 | + list_indices = convert_ROI_table_to_indices( |
| 89 | + adata, |
| 90 | + level=level, |
| 91 | + coarsening_xy=coarsening_xy, |
| 92 | + full_res_pxl_sizes_zyx=full_res_pxl_sizes_zyx, |
| 93 | + ) |
| 94 | + print() |
| 95 | + original_shape = ( |
| 96 | + NUM_Z_PLANES, |
| 97 | + 2 * IMG_SIZE_Y, |
| 98 | + 2 * IMG_SIZE_X // coarsening_xy**level, |
| 99 | + ) |
| 100 | + expected_shape = ( |
| 101 | + NUM_Z_PLANES, |
| 102 | + 2 * IMG_SIZE_Y // coarsening_xy**level, |
| 103 | + 2 * IMG_SIZE_X // coarsening_xy**level, |
| 104 | + ) |
| 105 | + print(f"Original shaep: {original_shape}") |
| 106 | + print(f"coarsening_xy={coarsening_xy}, level={level}") |
| 107 | + print(f"Expected shape: {expected_shape}") |
| 108 | + print("FOV-ROI indices:") |
| 109 | + for indices in list_indices: |
| 110 | + print(indices) |
| 111 | + print() |
| 112 | + |
| 113 | + assert list_indices[0][5] == list_indices[1][4] |
| 114 | + assert list_indices[0][3] == list_indices[2][2] |
| 115 | + assert ( |
| 116 | + abs( |
| 117 | + (list_indices[0][5] - list_indices[0][4]) |
| 118 | + - (list_indices[1][5] - list_indices[1][4]) |
| 119 | + ) |
| 120 | + < coarsening_xy |
| 121 | + ) |
| 122 | + assert ( |
| 123 | + abs( |
| 124 | + (list_indices[0][3] - list_indices[0][2]) |
| 125 | + - (list_indices[1][3] - list_indices[1][2]) |
| 126 | + ) |
| 127 | + < coarsening_xy |
| 128 | + ) |
| 129 | + assert abs(list_indices[1][5] - expected_shape[2]) < coarsening_xy |
| 130 | + assert abs(list_indices[2][3] - expected_shape[1]) < coarsening_xy |
| 131 | + for indices in list_indices: |
| 132 | + assert indices[0] == 0 |
| 133 | + assert indices[1] == NUM_Z_PLANES |
| 134 | + |
| 135 | + |
| 136 | +@pytest.mark.parametrize("level,coarsening_xy", list_params) |
| 137 | +def test_ROI_indices_split(level, coarsening_xy): |
| 138 | + |
| 139 | + metadata_dataframe = get_metadata_dataframe() |
| 140 | + adata = prepare_FOV_ROI_table(metadata_dataframe) |
| 141 | + |
| 142 | + full_res_pxl_sizes_zyx = [PIXEL_SIZE_Z, PIXEL_SIZE_Y, PIXEL_SIZE_X] |
| 143 | + list_indices = convert_ROI_table_to_indices( |
| 144 | + adata, |
| 145 | + level=level, |
| 146 | + coarsening_xy=coarsening_xy, |
| 147 | + full_res_pxl_sizes_zyx=full_res_pxl_sizes_zyx, |
| 148 | + ) |
| 149 | + |
| 150 | + list_indices = split_3D_indices_into_z_layers(list_indices) |
| 151 | + assert len(list_indices) == NUM_Z_PLANES * 2 * 2 |
| 152 | + |
| 153 | + |
| 154 | +@pytest.mark.parametrize("level,coarsening_xy", list_params) |
| 155 | +def test_ROI_indices_2D(level, coarsening_xy): |
| 156 | + |
| 157 | + metadata_dataframe = get_metadata_dataframe() |
| 158 | + adata = prepare_FOV_ROI_table(metadata_dataframe) |
| 159 | + adata = convert_FOV_ROIs_3D_to_2D(adata, PIXEL_SIZE_Z) |
| 160 | + |
| 161 | + full_res_pxl_sizes_zyx = [PIXEL_SIZE_Z, PIXEL_SIZE_Y, PIXEL_SIZE_X] |
| 162 | + list_indices = convert_ROI_table_to_indices( |
| 163 | + adata, |
| 164 | + level=level, |
| 165 | + coarsening_xy=coarsening_xy, |
| 166 | + full_res_pxl_sizes_zyx=full_res_pxl_sizes_zyx, |
| 167 | + ) |
| 168 | + |
| 169 | + for indices in list_indices: |
| 170 | + assert indices[0] == 0 |
| 171 | + assert indices[1] == 1 |
0 commit comments