celldetective 1.3.7.post1__py3-none-any.whl → 1.3.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. celldetective/_version.py +1 -1
  2. celldetective/gui/btrack_options.py +8 -8
  3. celldetective/gui/classifier_widget.py +8 -0
  4. celldetective/gui/configure_new_exp.py +1 -1
  5. celldetective/gui/json_readers.py +2 -4
  6. celldetective/gui/plot_signals_ui.py +38 -29
  7. celldetective/gui/process_block.py +1 -0
  8. celldetective/gui/processes/downloader.py +108 -0
  9. celldetective/gui/processes/measure_cells.py +346 -0
  10. celldetective/gui/processes/segment_cells.py +354 -0
  11. celldetective/gui/processes/track_cells.py +298 -0
  12. celldetective/gui/processes/train_segmentation_model.py +270 -0
  13. celldetective/gui/processes/train_signal_model.py +108 -0
  14. celldetective/gui/seg_model_loader.py +71 -25
  15. celldetective/gui/signal_annotator2.py +10 -7
  16. celldetective/gui/signal_annotator_options.py +1 -1
  17. celldetective/gui/tableUI.py +252 -20
  18. celldetective/gui/viewers.py +1 -1
  19. celldetective/io.py +53 -20
  20. celldetective/measure.py +12 -144
  21. celldetective/relative_measurements.py +40 -43
  22. celldetective/segmentation.py +48 -1
  23. celldetective/signals.py +84 -305
  24. celldetective/tracking.py +23 -24
  25. celldetective/utils.py +1 -1
  26. {celldetective-1.3.7.post1.dist-info → celldetective-1.3.8.dist-info}/METADATA +11 -2
  27. {celldetective-1.3.7.post1.dist-info → celldetective-1.3.8.dist-info}/RECORD +31 -25
  28. {celldetective-1.3.7.post1.dist-info → celldetective-1.3.8.dist-info}/WHEEL +1 -1
  29. {celldetective-1.3.7.post1.dist-info → celldetective-1.3.8.dist-info}/LICENSE +0 -0
  30. {celldetective-1.3.7.post1.dist-info → celldetective-1.3.8.dist-info}/entry_points.txt +0 -0
  31. {celldetective-1.3.7.post1.dist-info → celldetective-1.3.8.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,298 @@
1
+ from multiprocessing import Process
2
+ import time
3
+ import datetime
4
+ import os
5
+ import json
6
+ from celldetective.io import auto_load_number_of_frames, _load_frames_to_measure, locate_labels
7
+ from celldetective.utils import extract_experiment_channels, ConfigSectionMap, _get_img_num_per_channel, _mask_intensity_measurements
8
+ from pathlib import Path, PurePath
9
+ from glob import glob
10
+ from tqdm import tqdm
11
+ import numpy as np
12
+ import gc
13
+ import concurrent.futures
14
+ import datetime
15
+ import os
16
+ import json
17
+ from celldetective.io import interpret_tracking_configuration
18
+ from celldetective.utils import extract_experiment_channels
19
+ from celldetective.measure import drop_tonal_features, measure_features
20
+ from celldetective.tracking import track
21
+ import pandas as pd
22
+ from natsort import natsorted
23
+ from art import tprint
24
+
25
+
26
+ class TrackingProcess(Process):
27
+
28
+ def __init__(self, queue=None, process_args=None, *args, **kwargs):
29
+
30
+ super().__init__(*args, **kwargs)
31
+
32
+ self.queue = queue
33
+
34
+ if process_args is not None:
35
+ for key, value in process_args.items():
36
+ setattr(self, key, value)
37
+
38
+
39
+ tprint("Track")
40
+ self.timestep_dataframes = []
41
+
42
+ # Experiment
43
+ self.prepare_folders()
44
+
45
+ self.locate_experiment_config()
46
+ self.extract_experiment_parameters()
47
+ self.read_tracking_instructions()
48
+ self.detect_movie_and_labels()
49
+ self.detect_channels()
50
+
51
+ self.write_log()
52
+
53
+ if not self.btrack_option:
54
+ self.features = []
55
+ self.channel_names = None
56
+ self.haralick_options = None
57
+
58
+ self.sum_done = 0
59
+ self.t0 = time.time()
60
+
61
+ def read_tracking_instructions(self):
62
+
63
+ instr_path = PurePath(self.expfolder,Path(f"{self.instruction_file}"))
64
+ if os.path.exists(instr_path):
65
+ print(f"Tracking instructions for the {self.mode} population have been successfully loaded...")
66
+ with open(instr_path, 'r') as f:
67
+ self.instructions = json.load(f)
68
+
69
+ self.btrack_config = interpret_tracking_configuration(self.instructions['btrack_config_path'])
70
+
71
+ if 'features' in self.instructions:
72
+ self.features = self.instructions['features']
73
+ else:
74
+ self.features = None
75
+
76
+ if 'mask_channels' in self.instructions:
77
+ self.mask_channels = self.instructions['mask_channels']
78
+ else:
79
+ self.mask_channels = None
80
+
81
+ if 'haralick_options' in self.instructions:
82
+ self.haralick_options = self.instructions['haralick_options']
83
+ else:
84
+ self.haralick_options = None
85
+
86
+ if 'post_processing_options' in self.instructions:
87
+ self.post_processing_options = self.instructions['post_processing_options']
88
+ else:
89
+ self.post_processing_options = None
90
+
91
+ self.btrack_option = True
92
+ if 'btrack_option' in self.instructions:
93
+ self.btrack_option = self.instructions['btrack_option']
94
+ self.search_range = None
95
+ if 'search_range' in self.instructions:
96
+ self.search_range = self.instructions['search_range']
97
+ self.memory = None
98
+ if 'memory' in self.instructions:
99
+ self.memory = self.instructions['memory']
100
+ else:
101
+ print('Tracking instructions could not be located... Using a standard bTrack motion model instead...')
102
+ self.btrack_config = interpret_tracking_configuration(None)
103
+ self.features = None
104
+ self.mask_channels = None
105
+ self.haralick_options = None
106
+ self.post_processing_options = None
107
+ self.btrack_option = True
108
+ self.memory = None
109
+ self.search_range = None
110
+
111
+ if self.features is None:
112
+ self.features = []
113
+
114
+ def detect_channels(self):
115
+ self.img_num_channels = _get_img_num_per_channel(self.channel_indices, self.len_movie, self.nbr_channels)
116
+
117
+ def write_log(self):
118
+
119
+ features_log=f'features: {self.features}'
120
+ mask_channels_log=f'mask_channels: {self.mask_channels}'
121
+ haralick_option_log=f'haralick_options: {self.haralick_options}'
122
+ post_processing_option_log=f'post_processing_options: {self.post_processing_options}'
123
+ log_list=[features_log, mask_channels_log, haralick_option_log, post_processing_option_log]
124
+ log='\n'.join(log_list)
125
+
126
+ with open(self.pos+f'log_{self.mode}.txt', 'a') as f:
127
+ f.write(f'{datetime.datetime.now()} TRACK \n')
128
+ f.write(log+"\n")
129
+
130
+ def prepare_folders(self):
131
+
132
+ if not os.path.exists(self.pos+"output"):
133
+ os.mkdir(self.pos+"output")
134
+
135
+ if not os.path.exists(self.pos+os.sep.join(["output","tables"])):
136
+ os.mkdir(self.pos+os.sep.join(["output","tables"]))
137
+
138
+ if self.mode.lower()=="target" or self.mode.lower()=="targets":
139
+ self.label_folder = "labels_targets"
140
+ self.instruction_file = os.sep.join(["configs", "tracking_instructions_targets.json"])
141
+ self.napari_name = "napari_target_trajectories.npy"
142
+ self.table_name = "trajectories_targets.csv"
143
+
144
+ elif self.mode.lower()=="effector" or self.mode.lower()=="effectors":
145
+ self.label_folder = "labels_effectors"
146
+ self.instruction_file = os.sep.join(["configs","tracking_instructions_effectors.json"])
147
+ self.napari_name = "napari_effector_trajectories.npy"
148
+ self.table_name = "trajectories_effectors.csv"
149
+
150
+ def extract_experiment_parameters(self):
151
+
152
+ self.movie_prefix = ConfigSectionMap(self.config,"MovieSettings")["movie_prefix"]
153
+ self.spatial_calibration = float(ConfigSectionMap(self.config,"MovieSettings")["pxtoum"])
154
+ self.time_calibration = float(ConfigSectionMap(self.config,"MovieSettings")["frametomin"])
155
+ self.len_movie = float(ConfigSectionMap(self.config,"MovieSettings")["len_movie"])
156
+ self.shape_x = int(ConfigSectionMap(self.config,"MovieSettings")["shape_x"])
157
+ self.shape_y = int(ConfigSectionMap(self.config,"MovieSettings")["shape_y"])
158
+
159
+ self.channel_names, self.channel_indices = extract_experiment_channels(self.config)
160
+ self.nbr_channels = len(self.channel_names)
161
+
162
+ def locate_experiment_config(self):
163
+
164
+ parent1 = Path(self.pos).parent
165
+ self.expfolder = parent1.parent
166
+ self.config = PurePath(self.expfolder,Path("config.ini"))
167
+
168
+ if not os.path.exists(self.config):
169
+ print('The configuration file for the experiment was not found...')
170
+ self.abort_process()
171
+
172
+ def detect_movie_and_labels(self):
173
+
174
+ self.label_path = natsorted(glob(self.pos+f"{self.label_folder}"+os.sep+"*.tif"))
175
+ if len(self.label_path)>0:
176
+ print(f"Found {len(self.label_path)} segmented frames...")
177
+ else:
178
+ print(f"No segmented frames have been found. Please run segmentation first. Abort...")
179
+ self.abort_process()
180
+
181
+ try:
182
+ self.file = glob(self.pos+f"movie/{self.movie_prefix}*.tif")[0]
183
+ except IndexError:
184
+ self.file = None
185
+ self.haralick_option = None
186
+ self.features = drop_tonal_features(self.features)
187
+ print('Movie could not be found. Check the prefix.')
188
+
189
+ len_movie_auto = auto_load_number_of_frames(self.file)
190
+ if len_movie_auto is not None:
191
+ self.len_movie = len_movie_auto
192
+
193
+ def parallel_job(self, indices):
194
+
195
+ props = []
196
+
197
+ try:
198
+
199
+ for t in tqdm(indices,desc="frame"):
200
+
201
+ # Load channels at time t
202
+ img = _load_frames_to_measure(self.file, indices=self.img_num_channels[:,t])
203
+ lbl = locate_labels(self.pos, population=self.mode, frames=t)
204
+ if lbl is None:
205
+ continue
206
+
207
+ df_props = measure_features(img, lbl, features = self.features+['centroid'], border_dist=None,
208
+ channels=self.channel_names, haralick_options=self.haralick_options, verbose=False)
209
+ df_props.rename(columns={'centroid-1': 'x', 'centroid-0': 'y'},inplace=True)
210
+ df_props['t'] = int(t)
211
+
212
+ props.append(df_props)
213
+
214
+ self.sum_done+=1/self.len_movie*50
215
+ mean_exec_per_step = (time.time() - self.t0) / (self.sum_done*self.len_movie / 50 + 1)
216
+ pred_time = (self.len_movie - (self.sum_done*self.len_movie / 50 + 1)) * mean_exec_per_step + 30
217
+ self.queue.put([self.sum_done, pred_time])
218
+
219
+
220
+ except Exception as e:
221
+ print(e)
222
+
223
+ return props
224
+
225
+ def run(self):
226
+
227
+ self.indices = list(range(self.img_num_channels.shape[1]))
228
+ chunks = np.array_split(self.indices, self.n_threads)
229
+
230
+ self.timestep_dataframes = []
231
+ with concurrent.futures.ThreadPoolExecutor(max_workers=self.n_threads) as executor:
232
+ results = executor.map(self.parallel_job, chunks)
233
+ try:
234
+ for i,return_value in enumerate(results):
235
+ print(f'Thread {i} completed...')
236
+ #print(f"Thread {i} output check: ",return_value)
237
+ self.timestep_dataframes.extend(return_value)
238
+ except Exception as e:
239
+ print("Exception: ", e)
240
+
241
+ print('Features successfully measured...')
242
+
243
+ df = pd.concat(self.timestep_dataframes)
244
+ df.reset_index(inplace=True, drop=True)
245
+ df = _mask_intensity_measurements(df, self.mask_channels)
246
+
247
+ # do tracking
248
+ if self.btrack_option:
249
+ tracker = 'bTrack'
250
+ else:
251
+ tracker = 'trackpy'
252
+
253
+ # do tracking
254
+ trajectories, napari_data = track(None,
255
+ configuration=self.btrack_config,
256
+ objects=df,
257
+ spatial_calibration=self.spatial_calibration,
258
+ channel_names=self.channel_names,
259
+ return_napari_data=True,
260
+ optimizer_options = {'tm_lim': int(12e4)},
261
+ track_kwargs={'step_size': 100},
262
+ clean_trajectories_kwargs=self.post_processing_options,
263
+ volume=(self.shape_x, self.shape_y),
264
+ btrack_option=self.btrack_option,
265
+ search_range=self.search_range,
266
+ memory=self.memory,
267
+ )
268
+ print(f"Tracking successfully performed...")
269
+
270
+ # out trajectory table, create POSITION_X_um, POSITION_Y_um, TIME_min (new ones)
271
+ # Save napari data
272
+ np.save(self.pos+os.sep.join(['output', 'tables', self.napari_name]), napari_data, allow_pickle=True)
273
+
274
+ trajectories.to_csv(self.pos+os.sep.join(['output', 'tables', self.table_name]), index=False)
275
+ print(f"Trajectory table successfully exported in {os.sep.join(['output', 'tables'])}...")
276
+
277
+ if os.path.exists(self.pos+os.sep.join(['output', 'tables', self.table_name.replace('.csv','.pkl')])):
278
+ os.remove(self.pos+os.sep.join(['output', 'tables', self.table_name.replace('.csv','.pkl')]))
279
+
280
+ del trajectories; del napari_data;
281
+ gc.collect()
282
+
283
+ # Send end signal
284
+ self.queue.put([100, 0])
285
+ time.sleep(1)
286
+
287
+ self.queue.put("finished")
288
+ self.queue.close()
289
+
290
+ def end_process(self):
291
+
292
+ self.terminate()
293
+ self.queue.put("finished")
294
+
295
+ def abort_process(self):
296
+
297
+ self.terminate()
298
+ self.queue.put("error")
@@ -0,0 +1,270 @@
1
+ from multiprocessing import Process
2
+ import time
3
+ import os
4
+ import shutil
5
+ from glob import glob
6
+ import json
7
+ from tqdm import tqdm
8
+ import numpy as np
9
+ import random
10
+
11
+ from celldetective.utils import load_image_dataset, augmenter, interpolate_nan
12
+ from celldetective.io import normalize_multichannel
13
+ from stardist import fill_label_holes
14
+ from art import tprint
15
+ from distutils.dir_util import copy_tree
16
+ from csbdeep.utils import save_json
17
+
18
+
19
+ class TrainSegModelProcess(Process):
20
+
21
+ def __init__(self, queue=None, process_args=None, *args, **kwargs):
22
+
23
+ super().__init__(*args, **kwargs)
24
+
25
+ self.queue = queue
26
+
27
+ if process_args is not None:
28
+ for key, value in process_args.items():
29
+ setattr(self, key, value)
30
+
31
+ tprint("Train segmentation")
32
+ self.read_instructions()
33
+ self.extract_training_params()
34
+ self.load_dataset()
35
+ self.split_test_train()
36
+
37
+ self.sum_done = 0
38
+ self.t0 = time.time()
39
+
40
+ def read_instructions(self):
41
+
42
+ if os.path.exists(self.instructions):
43
+ with open(self.instructions, 'r') as f:
44
+ self.training_instructions = json.load(f)
45
+ else:
46
+ print('Training instructions could not be found. Abort.')
47
+ self.abort_process()
48
+
49
+ def run(self):
50
+
51
+ if self.model_type=="cellpose":
52
+ self.train_cellpose_model()
53
+ elif self.model_type=="stardist":
54
+ self.train_stardist_model()
55
+
56
+ self.queue.put("finished")
57
+ self.queue.close()
58
+
59
+ def train_stardist_model(self):
60
+
61
+ from stardist import calculate_extents, gputools_available
62
+ from stardist.models import Config2D, StarDist2D
63
+
64
+ n_rays = 32
65
+ print(gputools_available())
66
+
67
+ n_channel = self.X_trn[0].shape[-1]
68
+
69
+ # Predict on subsampled grid for increased efficiency and larger field of view
70
+ grid = (2,2)
71
+ conf = Config2D(
72
+ n_rays = n_rays,
73
+ grid = grid,
74
+ use_gpu = self.use_gpu,
75
+ n_channel_in = n_channel,
76
+ train_learning_rate = self.learning_rate,
77
+ train_patch_size = (256,256),
78
+ train_epochs = self.epochs,
79
+ train_reduce_lr = {'factor': 0.1, 'patience': 30, 'min_delta': 0},
80
+ train_batch_size = self.batch_size,
81
+ train_steps_per_epoch = int(self.augmentation_factor*len(self.X_trn)),
82
+ )
83
+
84
+ if self.use_gpu:
85
+ from csbdeep.utils.tf import limit_gpu_memory
86
+ limit_gpu_memory(None, allow_growth=True)
87
+
88
+ if self.pretrained is None:
89
+ model = StarDist2D(conf, name=self.model_name, basedir=self.target_directory)
90
+ else:
91
+ os.rename(self.instructions, os.sep.join([self.target_directory, self.model_name, 'temp.json']))
92
+ copy_tree(self.pretrained, os.sep.join([self.target_directory, self.model_name]))
93
+
94
+ if os.path.exists(os.sep.join([self.target_directory, self.model_name, 'training_instructions.json'])):
95
+ os.remove(os.sep.join([self.target_directory, self.model_name, 'training_instructions.json']))
96
+ if os.path.exists(os.sep.join([self.target_directory, self.model_name, 'config_input.json'])):
97
+ os.remove(os.sep.join([self.target_directory, self.model_name, 'config_input.json']))
98
+ if os.path.exists(os.sep.join([self.target_directory, self.model_name, 'logs'+os.sep])):
99
+ shutil.rmtree(os.sep.join([self.target_directory, self.model_name, 'logs']))
100
+ os.rename(os.sep.join([self.target_directory, self.model_name, 'temp.json']),os.sep.join([self.target_directory, self.model_name, 'training_instructions.json']))
101
+
102
+ #shutil.copytree(pretrained, os.sep.join([target_directory, model_name]))
103
+ model = StarDist2D(None, name=self.model_name, basedir=self.target_directory)
104
+ model.config.train_epochs = self.epochs
105
+ model.config.train_batch_size = min(len(self.X_trn),self.batch_size)
106
+ model.config.train_learning_rate = self.learning_rate # perf seems bad if lr is changed in transfer
107
+ model.config.use_gpu = self.use_gpu
108
+ model.config.train_reduce_lr = {'factor': 0.1, 'patience': 10, 'min_delta': 0}
109
+ print(f'{model.config=}')
110
+
111
+ save_json(vars(model.config), os.sep.join([self.target_directory, self.model_name, 'config.json']))
112
+
113
+ median_size = calculate_extents(list(self.Y_trn), np.mean)
114
+ fov = np.array(model._axes_tile_overlap('YX'))
115
+ print(f"median object size: {median_size}")
116
+ print(f"network field of view : {fov}")
117
+ if any(median_size > fov):
118
+ print("WARNING: median object size larger than field of view of the neural network.")
119
+
120
+ if self.augmentation_factor==1.0:
121
+ model.train(self.X_trn, self.Y_trn, validation_data=(self.X_val,self.Y_val))
122
+ else:
123
+ model.train(self.X_trn, self.Y_trn, validation_data=(self.X_val,self.Y_val), augmenter=augmenter)
124
+ model.optimize_thresholds(self.X_val,self.Y_val)
125
+
126
+ config_inputs = {"channels": self.target_channels, 'normalization_percentile': self.normalization_percentile,
127
+ 'normalization_clip': self.normalization_clip, 'normalization_values': self.normalization_values,
128
+ 'model_type': 'stardist', 'spatial_calibration': self.spatial_calibration, 'dataset': {'train': self.files_train, 'validation': self.files_val}}
129
+
130
+ json_input_config = json.dumps(config_inputs, indent=4)
131
+ with open(os.sep.join([self.target_directory, self.model_name, "config_input.json"]), "w") as outfile:
132
+ outfile.write(json_input_config)
133
+
134
+ def train_cellpose_model(self):
135
+
136
+ # do augmentation in place
137
+ X_aug = []; Y_aug = [];
138
+ n_val = max(1, int(round(self.augmentation_factor * len(self.X_trn))))
139
+ indices = random.choices(list(np.arange(len(self.X_trn))), k=n_val)
140
+ print('Performing image augmentation pre-training...')
141
+ for i in tqdm(indices):
142
+ x_aug,y_aug = augmenter(self.X_trn[i], self.Y_trn[i])
143
+ X_aug.append(x_aug)
144
+ Y_aug.append(y_aug)
145
+
146
+ # Channel axis in front for cellpose
147
+ X_aug = [np.moveaxis(x,-1,0) for x in X_aug]
148
+ self.X_val = [np.moveaxis(x,-1,0) for x in self.X_val]
149
+ print('number of augmented images: %3d' % len(X_aug))
150
+
151
+ from cellpose.models import CellposeModel
152
+ from cellpose.io import logger_setup
153
+ import torch
154
+
155
+ if not self.use_gpu:
156
+ print('Using CPU for training...')
157
+ device = torch.device("cpu")
158
+ else:
159
+ print('Using GPU for training...')
160
+
161
+ logger, log_file = logger_setup()
162
+ print(f'Pretrained model: ', self.pretrained)
163
+ if self.pretrained is not None:
164
+ pretrained_path = os.sep.join([self.pretrained,os.path.split(self.pretrained)[-1]])
165
+ else:
166
+ pretrained_path = self.pretrained
167
+
168
+ model = CellposeModel(gpu=self.use_gpu, model_type=None, pretrained_model=pretrained_path, diam_mean=30.0, nchan=X_aug[0].shape[0],)
169
+ model.train(train_data=X_aug, train_labels=Y_aug, normalize=False, channels=None, batch_size=self.batch_size,
170
+ min_train_masks=1,save_path=self.target_directory+os.sep+self.model_name,n_epochs=self.epochs, model_name=self.model_name, learning_rate=self.learning_rate, test_data = self.X_val, test_labels=self.Y_val)
171
+
172
+ file_to_move = glob(os.sep.join([self.target_directory, self.model_name, 'models','*']))[0]
173
+ shutil.move(file_to_move, os.sep.join([self.target_directory, self.model_name,''])+os.path.split(file_to_move)[-1])
174
+ os.rmdir(os.sep.join([self.target_directory, self.model_name, 'models']))
175
+
176
+ diameter = model.diam_labels
177
+
178
+ if self.pretrained is not None and os.path.split(self.pretrained)[-1]=='CP_nuclei':
179
+ standard_diameter = 17.0
180
+ else:
181
+ standard_diameter = 30.0
182
+
183
+ input_spatial_calibration = self.spatial_calibration #*diameter / standard_diameter
184
+
185
+ config_inputs = {"channels": self.target_channels, "diameter": standard_diameter, 'cellprob_threshold': 0., 'flow_threshold': 0.4,
186
+ 'normalization_percentile': self.normalization_percentile, 'normalization_clip': self.normalization_clip,
187
+ 'normalization_values': self.normalization_values, 'model_type': 'cellpose',
188
+ 'spatial_calibration': input_spatial_calibration, 'dataset': {'train': self.files_train, 'validation': self.files_val}}
189
+ json_input_config = json.dumps(config_inputs, indent=4)
190
+ with open(os.sep.join([self.target_directory, self.model_name, "config_input.json"]), "w") as outfile:
191
+ outfile.write(json_input_config)
192
+
193
+
194
+ def split_test_train(self):
195
+
196
+ if not len(self.X) > 1:
197
+ print("Not enough training data")
198
+ self.abort_process()
199
+
200
+ rng = np.random.RandomState()
201
+ ind = rng.permutation(len(self.X))
202
+ n_val = max(1, int(round(self.validation_split * len(ind))))
203
+ ind_train, ind_val = ind[:-n_val], ind[-n_val:]
204
+ self.X_val, self.Y_val = [self.X[i] for i in ind_val] , [self.Y[i] for i in ind_val]
205
+ self.X_trn, self.Y_trn = [self.X[i] for i in ind_train], [self.Y[i] for i in ind_train]
206
+
207
+ self.files_train = [self.filenames[i] for i in ind_train]
208
+ self.files_val = [self.filenames[i] for i in ind_val]
209
+
210
+ print('number of images: %3d' % len(self.X))
211
+ print('- training: %3d' % len(self.X_trn))
212
+ print('- validation: %3d' % len(self.X_val))
213
+
214
+ def extract_training_params(self):
215
+
216
+ self.model_name = self.training_instructions['model_name']
217
+ self.target_directory = self.training_instructions['target_directory']
218
+ self.model_type = self.training_instructions['model_type']
219
+ self.pretrained = self.training_instructions['pretrained']
220
+
221
+ self.datasets = self.training_instructions['ds']
222
+
223
+ self.target_channels = self.training_instructions['channel_option']
224
+ self.normalization_percentile = self.training_instructions['normalization_percentile']
225
+ self.normalization_clip = self.training_instructions['normalization_clip']
226
+ self.normalization_values = self.training_instructions['normalization_values']
227
+ self.spatial_calibration = self.training_instructions['spatial_calibration']
228
+
229
+ self.validation_split = self.training_instructions['validation_split']
230
+ self.augmentation_factor = self.training_instructions['augmentation_factor']
231
+
232
+ self.learning_rate = self.training_instructions['learning_rate']
233
+ self.epochs = self.training_instructions['epochs']
234
+ self.batch_size = self.training_instructions['batch_size']
235
+
236
+ def load_dataset(self):
237
+
238
+ print(f'Datasets: {self.datasets}')
239
+ self.X,self.Y,self.filenames = load_image_dataset(self.datasets, self.target_channels, train_spatial_calibration=self.spatial_calibration,
240
+ mask_suffix='labelled')
241
+ print('Dataset loaded...')
242
+
243
+ self.values = []
244
+ self.percentiles = []
245
+ for k in range(len(self.normalization_percentile)):
246
+ if self.normalization_percentile[k]:
247
+ self.percentiles.append(self.normalization_values[k])
248
+ self.values.append(None)
249
+ else:
250
+ self.percentiles.append(None)
251
+ self.values.append(self.normalization_values[k])
252
+
253
+ self.X = [normalize_multichannel(x, **{"percentiles": self.percentiles, 'values': self.values, 'clip': self.normalization_clip}) for x in self.X]
254
+
255
+ for k in range(len(self.X)):
256
+ x = self.X[k].copy()
257
+ x_interp = np.moveaxis([interpolate_nan(x[:,:,c].copy()) for c in range(x.shape[-1])],0,-1)
258
+ self.X[k] = x_interp
259
+
260
+ self.Y = [fill_label_holes(y) for y in tqdm(self.Y)]
261
+
262
+ def end_process(self):
263
+
264
+ self.terminate()
265
+ self.queue.put("finished")
266
+
267
+ def abort_process(self):
268
+
269
+ self.terminate()
270
+ self.queue.put("error")
@@ -0,0 +1,108 @@
1
+ from multiprocessing import Process
2
+ import time
3
+ import os
4
+ import json
5
+ from glob import glob
6
+ import numpy as np
7
+ from art import tprint
8
+ from celldetective.signals import SignalDetectionModel
9
+ from celldetective.io import locate_signal_model
10
+
11
+
12
+ class TrainSignalModelProcess(Process):
13
+
14
+ def __init__(self, queue=None, process_args=None, *args, **kwargs):
15
+
16
+ super().__init__(*args, **kwargs)
17
+
18
+ self.queue = queue
19
+
20
+ if process_args is not None:
21
+ for key, value in process_args.items():
22
+ setattr(self, key, value)
23
+
24
+ tprint("Train segmentation")
25
+ self.read_instructions()
26
+ self.extract_training_params()
27
+
28
+
29
+ self.sum_done = 0
30
+ self.t0 = time.time()
31
+
32
+ def read_instructions(self):
33
+
34
+ if os.path.exists(self.instructions):
35
+ with open(self.instructions, 'r') as f:
36
+ self.training_instructions = json.load(f)
37
+ else:
38
+ print('Training instructions could not be found. Abort.')
39
+ self.abort_process()
40
+
41
+ all_classes = []
42
+ for d in self.training_instructions["ds"]:
43
+ datasets = glob(d+os.sep+"*.npy")
44
+ for dd in datasets:
45
+ data = np.load(dd, allow_pickle=True)
46
+ classes = np.unique([ddd["class"] for ddd in data])
47
+ all_classes.extend(classes)
48
+ all_classes = np.unique(all_classes)
49
+ n_classes = len(all_classes)
50
+
51
+ self.model_params = {k:self.training_instructions[k] for k in ('pretrained', 'model_signal_length', 'channel_option', 'n_channels', 'label') if k in self.training_instructions}
52
+ self.model_params.update({'n_classes': n_classes})
53
+ self.train_params = {k:self.training_instructions[k] for k in ('model_name', 'target_directory', 'channel_option','recompile_pretrained', 'test_split', 'augment', 'epochs', 'learning_rate', 'batch_size', 'validation_split','normalization_percentile','normalization_values','normalization_clip') if k in self.training_instructions}
54
+
55
+ def neighborhood_postprocessing(self):
56
+
57
+ # if neighborhood of interest in training instructions, write it in config!
58
+ if 'neighborhood_of_interest' in self.training_instructions:
59
+ if self.training_instructions['neighborhood_of_interest'] is not None:
60
+
61
+ model_path = locate_signal_model(self.training_instructions['model_name'], path=None, pairs=True)
62
+ complete_path = model_path #+model
63
+ complete_path = rf"{complete_path}"
64
+ model_config_path = os.sep.join([complete_path,'config_input.json'])
65
+ model_config_path = rf"{model_config_path}"
66
+
67
+ f = open(model_config_path)
68
+ config = json.load(f)
69
+ config.update({'neighborhood_of_interest': self.training_instructions['neighborhood_of_interest'], 'reference_population': self.training_instructions['reference_population'], 'neighbor_population': self.training_instructions['neighbor_population']})
70
+ json_string = json.dumps(config)
71
+ with open(model_config_path, 'w') as outfile:
72
+ outfile.write(json_string)
73
+
74
+ def run(self):
75
+
76
+ model = SignalDetectionModel(**self.model_params)
77
+ model.fit_from_directory(self.training_instructions['ds'], **self.train_params)
78
+ self.neighborhood_postprocessing()
79
+ self.queue.put("finished")
80
+ self.queue.close()
81
+
82
+
83
+ def extract_training_params(self):
84
+
85
+ self.training_instructions.update({'n_channels': len(self.training_instructions['channel_option'])})
86
+ if self.training_instructions['augmentation_factor']>1.0:
87
+ self.training_instructions.update({'augment': True})
88
+ else:
89
+ self.training_instructions.update({'augment': False})
90
+ self.training_instructions.update({'test_split': 0.})
91
+
92
+
93
+ def end_process(self):
94
+
95
+ # self.terminate()
96
+
97
+ # if self.model_type=="stardist":
98
+ # from stardist.models import StarDist2D
99
+ # self.model = StarDist2D(None, name=self.model_name, basedir=self.target_directory)
100
+ # self.model.optimize_thresholds(self.X_val,self.Y_val)
101
+
102
+ self.terminate()
103
+ self.queue.put("finished")
104
+
105
+ def abort_process(self):
106
+
107
+ self.terminate()
108
+ self.queue.put("error")