-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclearMapWatershedParameters.py
More file actions
273 lines (189 loc) · 12 KB
/
clearMapWatershedParameters.py
File metadata and controls
273 lines (189 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# -*- coding: utf-8 -*-
"""
Example script to set up the parameters for the image processing pipeline
"""
######################### Import modules
import os, numpy, math
import ClearMap.Settings as settings
import ClearMap.IO as io
from ClearMap.Alignment.Resampling import resampleData;
from ClearMap.Alignment.Elastix import alignData, transformPoints
from ClearMap.ImageProcessing.CellDetection import detectCells
from ClearMap.Alignment.Resampling import resamplePoints, resamplePointsInverse
from ClearMap.Analysis.Label import countPointsInRegions
from ClearMap.Analysis.Voxelization import voxelize
from ClearMap.Analysis.Statistics import thresholdPoints
from ClearMap.Utils.ParameterTools import joinParameter
from ClearMap.Analysis.Label import labelToName
from multiprocessing import Process
######################### Data parameters
#Directory to save all the results, usually containing the data for one sample
BaseDirectory = '/d2/studies/ClearMap/IA_iDISCO/IA2_RT/';
#Data File and Reference channel File, usually as a sequence of files from the microscope
#Use \d{4} for 4 digits in the sequence for instance. As an example, if you have cfos-Z0001.ome.tif :
#os.path.join() is used to join the BaseDirectory path and the data paths:
cFosFile = os.path.join(BaseDirectory, 'IA2_RT_cfos_stack.ome.tif');
AutofluoFile = os.path.join(BaseDirectory, 'IA2_RT_auto_stack.ome.tif');
#Specify the range for the cell detection. This doesn't affect the resampling and registration operations
cFosFileRange = {'x' : all, 'y' : all, 'z' : all};
#Resolution of the Raw Data (in um / pixel)
OriginalResolution = (5,5,4.5);
#Orientation: 1,2,3 means the same orientation as the reference and atlas files.
#Flip axis with - sign (eg. (-1,2,3) flips x). 3D Rotate by swapping numbers. (eg. (2,1,3) swaps x and y)
FinalOrientation = (1,2,3);
#Resolution of the Atlas (in um/ pixel)
AtlasResolution = (25,25,25);
#Path to registration parameters and atlases
PathReg = '/d1/studies/ClearMap/ClearMap_resources/Parameter_files/';
AtlasFile = '/d2/studies/ClearMap/IA_iDISCO/Atlas_Horizontal_8-302_Full.tif';
AnnotationFile = '/d2/studies/ClearMap/IA_iDISCO/Annotations_Horizontal_8-302_Full.tif';
######################### Cell Detection Parameters using custom filters
#Spot detection method: faster, but optimised for spherical objects.
#You can also use "Ilastik" for more complex objects
ImageProcessingMethod = "SpotDetection";
#For illumination correction (necessitates a specific calibration curve for your microscope)
correctIlluminationParameter = {
"flatfield" : None, # (True or None) flat field intensities, if None do not correct image for illumination
"background" : None, # (None or array) background image as file name or array, if None background is assumed to be zero
"scaling" : "Mean", # (str or None) scale the corrected result by this factor, if 'max'/'mean' scale to keep max/mean invariant
"save" : None, # (str or None) save the corrected image to file
"verbose" : True # (bool or int) print / plot information about this step
}
#Remove the background with morphological opening (optimised for spherical objects)
removeBackgroundParameter = {
"size" : (8,8), # size in pixels (x,y) for the structure element of the morphological opening
"save" : None, #os.path.join(BaseDirectory, 'Background/background\d{4}.ome.tif'), # file name to save result of this operation
"verbose" : True # print / plot information about this step
}
#Difference of Gaussians filter: to enhance the edges. Useful if the objects have a non smooth texture (eg: amyloid deposits)
filterDoGParameter = {
"size" : (4,4,4), # (tuple or None) size for the DoG filter in pixels (x,y,z) if None, do not correct for any background
"sigma" : None, # (tuple or None) std of outer Gaussian, if None automatically determined from size
"sigma2" : None, # (tuple or None) std of inner Gaussian, if None automatically determined from size
"save" : None, #os.path.join(BaseDirectory, 'DoG/DoG\d{4}.ome.tif'), # (str or None) file name to save result of this operation if None dont save to file
"verbose" : True # (bool or int) print / plot information about this step
}
#Extended maxima: if the peak intensity in the object is surrounded by smaller peaks: avoids overcounting "granular" looking objects
findExtendedMaximaParameter = {
"hMax" : None, # (float or None) h parameter (for instance 20) for the initial h-Max transform, if None, do not perform a h-max transform
"size" : (6,6,6), # (tuple) size for the structure element for the local maxima filter
"threshold" : 10, # (float or None) include only maxima larger than a threshold, if None keep all local maxima
"save" : None, # (str or None) file name to save result of this operation if None dont save to file
"verbose" : True # (bool or int) print / plot information about this step
}
#If no cell shape detection and the maximum intensity is not at the gravity center of the object, look for a peak intensity around the center of mass.
findIntensityParameter = {
"method" : 'Max', # (str, func, None) method to use to determine intensity (e.g. "Max" or "Mean") if None take intensities at the given pixels
"size" : (6,6,4) # (tuple) size of the search box on which to perform the *method*
}
#Object volume detection. The object is painted by a watershed, until reaching the intensity threshold, based on the background subtracted image
detectCellShapeParameter = {
"threshold" : 125, # (float or None) threshold to determine mask. Pixels below this are background if None no mask is generated
"save" : None, #os.path.join(BaseDirectory, 'cellShape/cellShape\d{4}.ome.tif'), # (str or None) file name to save result of this operation if None dont save to file
"verbose" : True # (bool or int) print / plot information about this step if None take intensities at the given pixels
}
#to save: os.path.join(BaseDirectory, 'cellshape/cellshape\d{4}.ome.tif')
## Parameters for cell detection using spot detection algorithm
detectSpotsParameter = {
"correctIlluminationParameter" : correctIlluminationParameter,
"removeBackgroundParameter" : removeBackgroundParameter,
"filterDoGParameter" : filterDoGParameter,
"findExtendedMaximaParameter" : findExtendedMaximaParameter,
"findIntensityParameter" : findIntensityParameter,
"detectCellShapeParameter" : detectCellShapeParameter
}
#################### Heat map generation
##Voxelization: file name for the output:
VoxelizationFile = os.path.join(BaseDirectory, 'points_voxelized.tif');
# Parameter to calculate the density of the voxelization
voxelizeParameter = {
#Method to voxelize
"method" : 'Spherical', # Spherical,'Rectangular, Gaussian'
# Define bounds of the volume to be voxelized in pixels
"size" : (15,15,15),
# Voxelization weigths (e/g intensities)
"weights" : None
};
############################ Config parameters
#Processes to use for Resampling (usually twice the number of physical processors)
ResamplingParameter = {
"processes": 12,
};
#Stack Processing Parameter for cell detection
StackProcessingParameter = {
#max number of parallel processes. Be careful of the memory footprint of each process!
"processes" : 5,
#chunk sizes: number of planes processed at once
"chunkSizeMax" : 60,
"chunkSizeMin" : 10,
"chunkOverlap" : 6,
#optimize chunk size and number to number of processes to limit the number of cycles
"chunkOptimization" : True,
#increase chunk size for optimization (True, False or all = automatic)
"chunkOptimizationSize" : all,
"processMethod" : "parallel"
};
######################## Run Parameters, usually you don't need to change those
### Resample Fluorescent and CFos images
# Autofluorescent cFos resampling for aquisition correction
ResolutionAffineCFosAutoFluo = (16, 16, 16);
CorrectionResamplingParameterCfos = ResamplingParameter.copy();
CorrectionResamplingParameterCfos["source"] = cFosFile;
CorrectionResamplingParameterCfos["sink"] = os.path.join(BaseDirectory, 'cfos_resampled.tif');
CorrectionResamplingParameterCfos["resolutionSource"] = OriginalResolution;
CorrectionResamplingParameterCfos["resolutionSink"] = ResolutionAffineCFosAutoFluo;
CorrectionResamplingParameterCfos["orientation"] = FinalOrientation;
#Files for Auto-fluorescence for acquisition movements correction
CorrectionResamplingParameterAutoFluo = CorrectionResamplingParameterCfos.copy();
CorrectionResamplingParameterAutoFluo["source"] = AutofluoFile;
CorrectionResamplingParameterAutoFluo["sink"] = os.path.join(BaseDirectory, 'autofluo_for_cfos_resampled.tif');
#Files for Auto-fluorescence (Atlas Registration)
RegistrationResamplingParameter = CorrectionResamplingParameterAutoFluo.copy();
RegistrationResamplingParameter["sink"] = os.path.join(BaseDirectory, 'autofluo_resampled.tif');
RegistrationResamplingParameter["resolutionSink"] = AtlasResolution;
### Align cFos and Autofluo
CorrectionAlignmentParameter = {
#moving and reference images
"movingImage" : os.path.join(BaseDirectory, 'autofluo_for_cfos_resampled.tif'),
"fixedImage" : os.path.join(BaseDirectory, 'cfos_resampled.tif'),
#elastix parameter files for alignment
"affineParameterFile" : os.path.join(PathReg, 'Par0000affine_acquisition.txt'),
"bSplineParameterFile" : None,
#directory of the alignment result
"resultDirectory" : os.path.join(BaseDirectory, 'elastix_cfos_to_auto')
};
### Align Autofluo and Atlas
#directory of the alignment result
RegistrationAlignmentParameter = CorrectionAlignmentParameter.copy();
RegistrationAlignmentParameter["resultDirectory"] = os.path.join(BaseDirectory, 'elastix_auto_to_atlas');
#moving and reference images
RegistrationAlignmentParameter["movingImage"] = AtlasFile;
RegistrationAlignmentParameter["fixedImage"] = os.path.join(BaseDirectory, 'autofluo_resampled.tif');
#elastix parameter files for alignment
RegistrationAlignmentParameter["affineParameterFile"] = os.path.join(PathReg, 'Par0000affine.txt');
RegistrationAlignmentParameter["bSplineParameterFile"] = os.path.join(PathReg, 'Par0000bspline.txt');
# result files for cell coordinates (csv, vtk or ims)
SpotDetectionParameter = {
"source" : cFosFile,
"sink" : (os.path.join(BaseDirectory, 'cells-allpoints.npy'), os.path.join(BaseDirectory, 'intensities-allpoints.npy')),
"detectSpotsParameter" : detectSpotsParameter
};
SpotDetectionParameter = joinParameter(SpotDetectionParameter, cFosFileRange)
ImageProcessingParameter = joinParameter(StackProcessingParameter, SpotDetectionParameter);
FilteredCellsFile = (os.path.join(BaseDirectory, 'cells.npy'), os.path.join(BaseDirectory, 'intensities.npy'));
TransformedCellsFile = os.path.join(BaseDirectory, 'cells_transformed_to_Atlas.npy')
### Transform points from Original c-Fos position to autofluorescence
## Transform points from original to corrected
# downscale points to referenece image size
CorrectionResamplingPointsParameter = CorrectionResamplingParameterCfos.copy();
CorrectionResamplingPointsParameter["pointSource"] = os.path.join(BaseDirectory, 'cells.npy');
CorrectionResamplingPointsParameter["dataSizeSource"] = cFosFile;
CorrectionResamplingPointsParameter["pointSink"] = None;
CorrectionResamplingPointsInverseParameter = CorrectionResamplingPointsParameter.copy();
CorrectionResamplingPointsInverseParameter["dataSizeSource"] = cFosFile;
CorrectionResamplingPointsInverseParameter["pointSink"] = None;
## Transform points from corrected to registered
# downscale points to referenece image size
RegistrationResamplingPointParameter = RegistrationResamplingParameter.copy();
RegistrationResamplingPointParameter["dataSizeSource"] = cFosFile;
RegistrationResamplingPointParameter["pointSink"] = None;