/
clearMap_Ilastik_parameters.py
286 lines (197 loc) · 11.3 KB
/
clearMap_Ilastik_parameters.py
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
273
274
275
276
277
278
279
280
281
282
283
284
# -*- coding: utf-8 -*-
"""
Example script to set up the parameters for the image processing pipeline.
Note even though variables indicate 'signalFile', the signal channel can be whatever you are looking at.
Just too much work to update all of the variables in the package to indicate other than cFos.
"""
######################### Import modules
import os, numpy, math
os.chdir('/d1/software/ClearMap/')
import pandas as pd
from scipy.stats import zscore
from datetime import datetime as dt
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
######################### Data parameters
#Directory to save all the results, usually containing the data for one sample
sampleName='BH2-4'
today = dt.now().strftime("%m-%d-%Y")
analysisLabel=today # this will be appended to all result file names
saveName = '_'.join([sampleName, analysisLabel])
testRegion='all'
signalChannel='tdTomato'
prefix = '_'.join([sampleName, signalChannel])
BaseDirectory = '/d2/studies/CM2/SmartSPIM/BH_Npas4_TRAP/'+sampleName+'_Stitched/'
#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:
signalFile = os.path.join(BaseDirectory, 'stacks', sampleName+'_Stitched_'+signalChannel+'.ome.tif')
AutofluoFile = os.path.join(BaseDirectory, 'stacks', sampleName+'Stitched_auto.ome.tif')
#Specify the range for the cell detection. This doesn't affect the resampling and registration operations
if testRegion=='all':
signalFileRange = {'x' : all, 'y' : all, 'z' : all};
elif testRegion=='pfc':
signalFileRange = {'x' : (1225,1525), 'y' : (350,650), 'z' : (800,900)};
elif testRegion=='hpc':
signalFileRange = {'x' : (2100,2400), 'y' : (1500,1800), 'z' : (700,800)};
#Resolution of the Raw Data (in um / pixel)OriginalResolution = (5.0, 5.0, 4.5)
OriginalResolution = (4.0, 4.0, 4.0)
#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 = (20, 20, 20)
#Path to registration parameters and atlases
PathReg = '/d2/studies/ClearMap/Parameter_files/Parameter_Files_Perens';
AtlasFile = '/d2/studies/CM2/SmartSPIM/LSFM_Atlas_Cropped.ome.tif';
AnnotationFile = '/d2/studies/CM2/SmartSPIM/LSFM_Annotations_Cropped.ome.tif';
regionIndex = '/d2/studies/ClearMap/RegionID_Index.csv'
volumeIndex = '/d2/studies/ClearMap/RegionVolumeIndex.csv'
######################### 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 = "Ilastik"
ilastikParameter = {
"classifier" : "/d2/studies/CM2/SmartSPIM/BH_Npas4_TRAP/ilastik/BH_Npas4_TRAP_Classifier_Aug23.ilp",
"classindex" : 0,
"save" : os.path.join(BaseDirectory, 'IlastikClasses/',sampleName+'_IlastikClasses_Z\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 the maximum instensity is not at the gravity center of the object, look for a peak intensity around the center of mass.
findCellIntensityParameter = {
"method" : 'Max', # (str, func, None) method to use to determine intensity (e.g. "Max" or "Mean") if None take intensities at the given pixels
"verbose": True # (bool or int) print / plot information about this step
}
## Parameters for cell detection using spot detection algorithm
detectCellsParameter = {
"classifyCellsParameter" : ilastikParameter,
"findCellIntensityParameter" : findCellIntensityParameter
}
#################### Heat map generation
##Voxelization: file name for the output:
VoxelizationFile = os.path.join(BaseDirectory, sampleName+'_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": 18
}
#Stack Processing Parameter for cell detection
StackProcessingParameter = {
#max number of parallel processes. Be careful of the memory footprint of each process!
"processes" : 8,
#chunk sizes: number of planes processed at once
"chunkSizeMax" : 2000,
"chunkSizeMin" : 200,
"chunkOverlap" : 20,
#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
ResolutionAffinesignalAutoFluo = (16, 16, 16)
CorrectionResamplingParametersignal = ResamplingParameter.copy()
CorrectionResamplingParametersignal["source"] = signalFile
CorrectionResamplingParametersignal["sink"] = os.path.join(BaseDirectory, sampleName+'_'+signalChannel+'_resampled.tif')
CorrectionResamplingParametersignal["resolutionSource"] = OriginalResolution
CorrectionResamplingParametersignal["resolutionSink"] = ResolutionAffinesignalAutoFluo
CorrectionResamplingParametersignal["orientation"] = FinalOrientation
#Files for Auto-fluorescence for acquisition movements correction
CorrectionResamplingParameterAutoFluo = CorrectionResamplingParametersignal.copy();
CorrectionResamplingParameterAutoFluo["source"] = AutofluoFile;
CorrectionResamplingParameterAutoFluo["sink"] = os.path.join(BaseDirectory, sampleName+'_autofluo_for_signal_resampled.tif');
#Files for Auto-fluorescence (Atlas Registration)
RegistrationResamplingParameter = CorrectionResamplingParameterAutoFluo.copy();
RegistrationResamplingParameter["sink"] = os.path.join(BaseDirectory, sampleName+'_autofluo_resampled.tif');
RegistrationResamplingParameter["resolutionSink"] = AtlasResolution;
### Align cFos and Autofluo
CorrectionAlignmentParameter = {
#moving and reference images
"movingImage" : os.path.join(BaseDirectory, sampleName+'_autofluo_for_signal_resampled.tif'),
"fixedImage" : os.path.join(BaseDirectory, sampleName+'_'+signalChannel+'_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_signal_to_auto')
}
### Align Autofluo and Atlas
CorrectionAlignmentParameter = {
#moving and reference images
"movingImage" : os.path.join(BaseDirectory, sampleName+'_autofluo_for_signal_resampled.tif'),
"fixedImage" : os.path.join(BaseDirectory, sampleName+'_'+signalChannel+'_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_signal_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, sampleName+'_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)
CellDetectionParameter = {
"method" : ImageProcessingMethod,
"source" : signalFile,
"sink" : (os.path.join(BaseDirectory, sampleName+'_cells-allpoints_'+today+'.npy'), os.path.join(BaseDirectory, sampleName+'_intensities-allpoints_'+today+'.npy')),
}
CellDetectionParameter = joinParameter(CellDetectionParameter, detectCellsParameter)
CellDetectionParameter = joinParameter(CellDetectionParameter, signalFileRange)
ImageProcessingParameter = joinParameter(StackProcessingParameter, CellDetectionParameter)
FilteredCellsFile = (os.path.join(BaseDirectory, sampleName+'_cells_'+today+'.npy'), os.path.join(BaseDirectory, sampleName+'_intensities'+today+'.npy'))
TransformedCellsFile = os.path.join(BaseDirectory, sampleName+'_cells_transformed_to_Atlas_'+today+'.npy')
### Transform points from Original c-Fos position to autofluorescence
## Transform points from original to corrected
# downscale points to referenece image size
CorrectionResamplingPointsParameter = CorrectionResamplingParametersignal.copy();
CorrectionResamplingPointsParameter["pointSource"] = os.path.join(BaseDirectory, sampleName+'_cells_'+today+'.npy');
CorrectionResamplingPointsParameter["dataSizeSource"] = signalFile;
CorrectionResamplingPointsParameter["pointSink"] = None;
CorrectionResamplingPointsInverseParameter = CorrectionResamplingPointsParameter.copy();
CorrectionResamplingPointsInverseParameter["dataSizeSource"] = signalFile;
CorrectionResamplingPointsInverseParameter["pointSink"] = None;
## Transform points from corrected to registered
# downscale points to referenece image size
RegistrationResamplingPointParameter = RegistrationResamplingParameter.copy();
RegistrationResamplingPointParameter["dataSizeSource"] = signalFile;
RegistrationResamplingPointParameter["pointSink"] = None;
def printTime():
print(dt.now().strftime('%H:%M:%S'))
return(dt.now().strftime('%H:%M:%S'))
def writeRunParameters():
date = dt.now().strftime("%Y-%m-%d")
time = dt.now().strftime('%H:%M:%S')
if ImageProcessingMethod=='ilastik':
writeString = str(ilastikParameter)
params = str(date + '\n' + time + '\n Sample: ' + str(sampleName) + '\n ImageProcessingMethod: ' + str(ImageProcessingMethod) + '\n Classifier: ' + str(ilastikParameter['classifier']))
with open(os.path.join(BaseDirectory, 'runParams_'+date+'_'+time), 'w') as f:
f.write(params)
f.close()