|
1 | | -jobs = [] |
| 1 | +from cellmap_flow.norm.input_normalize import MinMaxNormalizer |
| 2 | +from cellmap_flow.post.postprocessors import DefaultPostprocessor |
| 3 | +from cellmap_flow.models.model_yaml import load_model_paths |
| 4 | +import os |
| 5 | +import threading |
| 6 | +import numpy as np |
2 | 7 |
|
3 | | -models_config = [] |
4 | 8 |
|
5 | | -servers = [] |
| 9 | +class Flow: |
| 10 | + _instance = None |
6 | 11 |
|
7 | | -raw = None |
| 12 | + def __new__(cls): |
| 13 | + if cls._instance is None: |
| 14 | + cls._instance = super(Flow, cls).__new__(cls) |
| 15 | + cls._instance.jobs = [] |
| 16 | + cls._instance.models_config = [] |
| 17 | + cls._instance.servers = [] |
| 18 | + cls._instance.raw = None |
| 19 | + cls._instance.input_norms = [] # or [MinMaxNormalizer(0, 255)] |
| 20 | + cls._instance.postprocess = [] |
| 21 | + cls._instance.viewer = None |
| 22 | + cls._instance.dataset_path = None |
| 23 | + cls._instance.model_catalog = {} |
| 24 | + # Uncomment and adjust if you want to load the model catalog: |
| 25 | + # cls._instance.model_catalog = load_model_paths( |
| 26 | + # os.path.normpath(os.path.join(os.path.dirname(__file__), os.pardir, "models", "models.yaml")) |
| 27 | + # ) |
| 28 | + cls._instance.queue = "gpu_h100" |
| 29 | + cls._instance.charge_group = "cellmap" |
| 30 | + cls._instance.neuroglancer_thread = None |
| 31 | + return cls._instance |
8 | 32 |
|
| 33 | + def to_dict(self): |
| 34 | + return self.__dict__.items() |
9 | 35 |
|
10 | | -from cellmap_flow.norm.input_normalize import MinMaxNormalizer |
11 | | -from cellmap_flow.post.postprocessors import DefaultPostprocessor |
| 36 | + def __repr__(self): |
| 37 | + return f"Flow({self.__dict__})" |
12 | 38 |
|
13 | | -# input_norms = [MinMaxNormalizer()] |
14 | | -# postprocess = [DefaultPostprocessor(0,200,0,1)] |
| 39 | + def __str__(self): |
| 40 | + return f"Flow({self.__dict__})" |
15 | 41 |
|
16 | | -input_norms = [] |
17 | | -postprocess = [] |
18 | | -viewer = None |
| 42 | + def get_output_dtype(self): |
| 43 | + dtype = np.float32 |
19 | 44 |
|
20 | | -dataset_path = None |
| 45 | + if len(self.input_norms) > 0: |
| 46 | + for norm in self.input_norms[::-1]: |
| 47 | + if norm.dtype: |
| 48 | + dtype = norm.dtype |
| 49 | + break |
21 | 50 |
|
| 51 | + if len(self.postprocess) > 0: |
| 52 | + for postprocess in self.postprocess[::-1]: |
| 53 | + if postprocess.dtype: |
| 54 | + dtype = postprocess.dtype |
| 55 | + break |
22 | 56 |
|
23 | | -from cellmap_flow.models.model_yaml import load_model_paths |
| 57 | + return dtype |
24 | 58 |
|
25 | | -import os |
26 | | -model_catalog = {} |
27 | | -# model_catalog = load_model_paths( |
28 | | -# os.path.normpath( |
29 | | -# os.path.join(os.path.dirname(__file__), os.pardir, "models", "models.yaml") |
30 | | -# ) |
31 | | -# ) |
32 | | - |
33 | | -queue = "gpu_h100" |
34 | | -charge_group = "cellmap" |
| 59 | + @classmethod |
| 60 | + def run( |
| 61 | + cls, |
| 62 | + zarr_path, |
| 63 | + model_configs, |
| 64 | + queue="gpu_h100", |
| 65 | + charge_group="cellmap", |
| 66 | + input_normalizers=None, |
| 67 | + post_processors=None, |
| 68 | + ): |
| 69 | + |
| 70 | + from cellmap_flow.utils.bsub_utils import start_hosts, SERVER_COMMAND |
| 71 | + from cellmap_flow.utils.neuroglancer_utils import generate_neuroglancer_url |
| 72 | + |
| 73 | + if input_normalizers is None: |
| 74 | + input_normalizers = [] |
| 75 | + if post_processors is None: |
| 76 | + post_processors = [] |
| 77 | + |
| 78 | + # Get the singleton instance (creates one if it doesn't exist) |
| 79 | + instance = cls() |
| 80 | + instance.queue = queue |
| 81 | + instance.charge_group = charge_group |
| 82 | + instance.dataset_path = zarr_path |
| 83 | + instance.input_norms = input_normalizers |
| 84 | + instance.postprocess = post_processors |
| 85 | + instance.models_config = model_configs |
| 86 | + instance.neuroglancer_thread = None |
| 87 | + |
| 88 | + threads = [] |
| 89 | + |
| 90 | + for model_config in instance.models_config: |
| 91 | + model_command = model_config.command |
| 92 | + command = f"{SERVER_COMMAND} {model_command} -d {instance.dataset_path}" |
| 93 | + print(f"Starting server with command: {command}") |
| 94 | + thread = threading.Thread( |
| 95 | + target=start_hosts, |
| 96 | + args=(command, queue, charge_group, model_config.name), |
| 97 | + ) |
| 98 | + thread.start() |
| 99 | + threads.append(thread) |
| 100 | + |
| 101 | + for thread in threads: |
| 102 | + thread.join() |
| 103 | + |
| 104 | + instance.neuroglancer_thread = threading.Thread( |
| 105 | + target=generate_neuroglancer_url, args=(instance.dataset_path,) |
| 106 | + ) |
| 107 | + instance.neuroglancer_thread.start() |
| 108 | + # Optionally wait for the neuroglancer thread: |
| 109 | + # instance.neuroglancer_thread.join() |
| 110 | + |
| 111 | + print(f"*****Neuroglancer URL: {instance.dataset_path}") |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def stop(cls): |
| 115 | + instance = cls() |
| 116 | + for job in instance.jobs: |
| 117 | + print(f"Killing job {job.job_id}") |
| 118 | + job.kill() |
| 119 | + if instance.neuroglancer_thread is not None: |
| 120 | + instance.neuroglancer_thread = None |
| 121 | + instance.jobs = [] |
| 122 | + |
| 123 | + @classmethod |
| 124 | + def delete(cls): |
| 125 | + cls._instance = None |
| 126 | + |
| 127 | + |
| 128 | +g = Flow() |
0 commit comments