|
| 1 | +import numpy as np |
| 2 | +from argparse import ArgumentParser |
| 3 | + |
| 4 | +nanometer = 1e-9 |
| 5 | + |
| 6 | + |
| 7 | +def create_gridded_config( |
| 8 | + sources, grid_locations, dim_names, output_dim_names, x_size, y_size, z_size |
| 9 | +): |
| 10 | + source_configs = [] |
| 11 | + total_n_dims = len(dim_names) |
| 12 | + xyz_index_mapping = [output_dim_names.index(dim) for dim in ["x", "y", "z"]] |
| 13 | + |
| 14 | + input_dimensions = [[1, ""]] * total_n_dims |
| 15 | + for i, dim_name in enumerate(output_dim_names): |
| 16 | + if dim_name == "x" or dim_name == "y" or dim_name == "z": |
| 17 | + input_dimensions[i] = [nanometer, "m"] |
| 18 | + elif dim_name == "t": |
| 19 | + input_dimensions[i] = [1, "s"] |
| 20 | + elif dim_name == "c'": |
| 21 | + input_dimensions[i] = [1, ""] |
| 22 | + |
| 23 | + dim_config = {name: val for name, val in zip(output_dim_names, input_dimensions)} |
| 24 | + input_dim_config = {name: val for name, val in zip(dim_names, input_dimensions)} |
| 25 | + |
| 26 | + for source, grid_location in zip(sources, grid_locations): |
| 27 | + source_config = { |
| 28 | + "url": source, |
| 29 | + "transform": { |
| 30 | + "outputDimensions": dim_config, |
| 31 | + "inputDimensions": input_dim_config, |
| 32 | + }, |
| 33 | + } |
| 34 | + source_configs.append(source_config) |
| 35 | + if grid_location == (0, 0, 0): |
| 36 | + continue |
| 37 | + transform_x = grid_location[0] * x_size |
| 38 | + transform_y = grid_location[1] * y_size |
| 39 | + transform_z = grid_location[2] * z_size |
| 40 | + transform_matrix = np.eye(total_n_dims).tolist() |
| 41 | + for i in range(total_n_dims): |
| 42 | + transform_matrix[i].append(0.0) |
| 43 | + transform_matrix[xyz_index_mapping[0]][-1] = transform_x |
| 44 | + transform_matrix[xyz_index_mapping[1]][-1] = transform_y |
| 45 | + transform_matrix[xyz_index_mapping[2]][-1] = transform_z |
| 46 | + source_config["transform"]["matrix"] = transform_matrix |
| 47 | + |
| 48 | + return source_configs, dim_config |
| 49 | + |
| 50 | + |
| 51 | +def wrap_config_in_image_layer(source_config, dim_config, final_dim_names): |
| 52 | + combined_config_dim = {k: dim_config[k] for k in final_dim_names if k in dim_config} |
| 53 | + return { |
| 54 | + "dimensions": combined_config_dim, |
| 55 | + "layers": [ |
| 56 | + {"type": "image", "source": source_config, "name": "Gridded Image Layer"} |
| 57 | + ], |
| 58 | + "layout": "4panel-alt", |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +def output_layer_as_json(layer): |
| 63 | + import json |
| 64 | + |
| 65 | + return json.dumps(layer, indent=4) |
| 66 | + |
| 67 | + |
| 68 | +def wrap_source_in_ngauth(source_name, ngauth_server, format="zarr3"): |
| 69 | + end = f"|{format}" |
| 70 | + start = "gs+ngauth+https://" |
| 71 | + start += ngauth_server + "/" |
| 72 | + source_name = source_name.replace("gs://", start) |
| 73 | + return source_name + end |
| 74 | + |
| 75 | + |
| 76 | +def read_sources_from_file(file_path): |
| 77 | + sources = [] |
| 78 | + with open(file_path, "r") as file: |
| 79 | + for line in file: |
| 80 | + source = line.strip() |
| 81 | + if source: |
| 82 | + sources.append(source) |
| 83 | + return sources |
| 84 | + |
| 85 | + |
| 86 | +def parse_grid_locations(sources): |
| 87 | + grid_locations = [] |
| 88 | + for source in sources: |
| 89 | + parts = source.strip("/").split("/") |
| 90 | + fname = parts[-1] |
| 91 | + # Format is XYZ_rROWcCOL |
| 92 | + row_id_start = fname.find("r") + 1 |
| 93 | + row_id = int(fname[row_id_start : row_id_start + 2]) |
| 94 | + col_id_start = fname.find("c") + 1 |
| 95 | + col_id = int(fname[col_id_start : col_id_start + 2]) |
| 96 | + grid_locations.append((col_id, row_id, 0)) # Assuming z=0 for simplicity |
| 97 | + return grid_locations |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + parser = ArgumentParser(description="Create a gridded image layer configuration.") |
| 102 | + parser.add_argument( |
| 103 | + "--ngauth-server", |
| 104 | + "-n", |
| 105 | + type=str, |
| 106 | + help="NGAUTH server to use for authentication.", |
| 107 | + required=True, |
| 108 | + ) |
| 109 | + parser.add_argument( |
| 110 | + "--format", |
| 111 | + "-f", |
| 112 | + type=str, |
| 113 | + default="zarr3", |
| 114 | + help="Format of the source data (default: zarr3).", |
| 115 | + ) |
| 116 | + parser.add_argument( |
| 117 | + "--output-dim-names", |
| 118 | + "-o", |
| 119 | + type=str, |
| 120 | + nargs="+", |
| 121 | + default=["z", "t", "c'", "y", "x"], |
| 122 | + help="Names of the dimensions in the data.", |
| 123 | + ) |
| 124 | + parser.add_argument( |
| 125 | + "--dim-names", |
| 126 | + "-d", |
| 127 | + type=str, |
| 128 | + nargs="+", |
| 129 | + default=["dim_0", "dim_1", "dim_2", "dim_3", "dim_4"], |
| 130 | + help="Names of the dimensions in the data.", |
| 131 | + ) |
| 132 | + parser.add_argument( |
| 133 | + "--final-dim-names", |
| 134 | + "-D", |
| 135 | + type=str, |
| 136 | + nargs="+", |
| 137 | + default=["x", "y", "t", "z"], |
| 138 | + ) |
| 139 | + parser.add_argument( |
| 140 | + "--xyz-size", |
| 141 | + "-s", |
| 142 | + type=int, |
| 143 | + nargs=3, |
| 144 | + default=[1, 1, 1], |
| 145 | + help="Size of the x, y, and z dimensions in voxels (default: 1, 1, 1).", |
| 146 | + ) |
| 147 | + parser.add_argument( |
| 148 | + "--num_sources", |
| 149 | + "-k", |
| 150 | + type=int, |
| 151 | + default=None, |
| 152 | + help="Number of sources to process (default: None, process all sources).", |
| 153 | + ) |
| 154 | + parser.add_argument( |
| 155 | + "source_file", |
| 156 | + type=str, |
| 157 | + help="File containing the list of sources, one per line.", |
| 158 | + ) |
| 159 | + args = parser.parse_args() |
| 160 | + ngauth_server = args.ngauth_server |
| 161 | + format_ = args.format |
| 162 | + source_file = args.source_file |
| 163 | + if args.num_sources is not None: |
| 164 | + args.num_sources = int(args.num_sources) |
| 165 | + sources = read_sources_from_file(source_file) |
| 166 | + if args.num_sources is not None: |
| 167 | + sources = sources[: args.num_sources] |
| 168 | + grid_locations = parse_grid_locations(sources) |
| 169 | + sources = [ |
| 170 | + wrap_source_in_ngauth(source, ngauth_server, format_) for source in sources |
| 171 | + ] |
| 172 | + output_dim_names = args.output_dim_names |
| 173 | + dim_names = args.dim_names |
| 174 | + final_dim_names = args.final_dim_names |
| 175 | + x_size, y_size, z_size = args.xyz_size |
| 176 | + |
| 177 | + cfg, dim_cfg = create_gridded_config( |
| 178 | + sources, grid_locations, dim_names, output_dim_names, x_size, y_size, z_size |
| 179 | + ) |
| 180 | + image_layer = wrap_config_in_image_layer(cfg, dim_cfg, final_dim_names) |
| 181 | + json_output = output_layer_as_json(image_layer) |
| 182 | + # write the output to a file |
| 183 | + with open("gridded_image_layer.json", "w") as f: |
| 184 | + f.write(json_output) |
0 commit comments