This repository has been archived by the owner on Nov 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
404 lines (368 loc) · 12.4 KB
/
__init__.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# coding=utf-8
"""
Modules - pipeline processing modules for CellProfiler
"""
import logging
logger = logging.getLogger(__name__)
import re
import sys
import cellprofiler.module as cpm
from cellprofiler.modules.plugins import plugin_list
from cellprofiler.preferences import get_plugin_directory
# python modules and their corresponding cellprofiler.module classes
pymodule_to_cpmodule = {
"align": "Align",
"calculatemath": "CalculateMath",
"calculatestatistics": "CalculateStatistics",
"classifyobjects": "ClassifyObjects",
"classify": "Classify",
"classifypixelsunet": "ClassifyPixelsUnet",
"closing": "Closing",
"colortogray": "ColorToGray",
"convertimagetoobjects": "ConvertImageToObjects",
"convertobjectstoimage": "ConvertObjectsToImage",
"correctilluminationcalculate": "CorrectIlluminationCalculate",
"correctilluminationapply": "CorrectIlluminationApply",
"createbatchfiles": "CreateBatchFiles",
"crop": "Crop",
"definegrid": "DefineGrid",
"dilateimage": "DilateImage",
"dilateobjects": "DilateObjects",
"displaydensityplot": "DisplayDensityPlot",
"displaydataonimage": "DisplayDataOnImage",
"displayhistogram": "DisplayHistogram",
"displayplatemap": "DisplayPlatemap",
"displayscatterplot": "DisplayScatterPlot",
"editobjectsmanually": "EditObjectsManually",
"enhanceedges": "EnhanceEdges",
"enhanceorsuppressfeatures": "EnhanceOrSuppressFeatures",
"erosion": "Erosion",
"expandorshrinkobjects": "ExpandOrShrinkObjects",
"exporttodatabase": "ExportToDatabase",
"exporttospreadsheet": "ExportToSpreadsheet",
"fillobjects": "FillObjects",
"filterobjects": "FilterObjects",
"flagimage": "FlagImage",
"flipandrotate": "FlipAndRotate",
"gaussianfilter": "GaussianFilter",
"graytocolor": "GrayToColor",
"groups": "Groups",
"identifydeadworms": "IdentifyDeadWorms",
"identifyobjectsingrid": "IdentifyObjectsInGrid",
"identifyobjectsmanually": "IdentifyObjectsManually",
"identifyprimaryobjects": "IdentifyPrimaryObjects",
"identifysecondaryobjects": "IdentifySecondaryObjects",
"identifytertiaryobjects": "IdentifyTertiaryObjects",
"imagemath": "ImageMath",
"images": "Images",
"invertforprinting": "InvertForPrinting",
"labelimages": "LabelImages",
"loadimages": "LoadImages",
"loadsingleimage": "LoadSingleImage",
"loaddata": "LoadData",
"makeprojection": "MakeProjection",
"maskimage": "MaskImage",
"maskobjects": "MaskObjects",
"measurecolocalization": "MeasureColocalization",
"measuregranularity": "MeasureGranularity",
"measureimageareaoccupied": "MeasureImageAreaOccupied",
"measureimagegranularity": "MeasureGranularity",
"measureimageintensity": "MeasureImageIntensity",
"measureimageoverlap": "MeasureImageOverlap",
"measureimagequality": "MeasureImageQuality",
"measureimageskeleton": "MeasureImageSkeleton",
"measureobjectintensity": "MeasureObjectIntensity",
"measureobjectoverlap": "MeasureObjectOverlap",
"measureobjectsizeshape": "MeasureObjectSizeShape",
"measureobjectneighbors": "MeasureObjectNeighbors",
"measureobjectintensitydistribution": "MeasureObjectIntensityDistribution",
"measureobjectskeleton": "MeasureObjectSkeleton",
"measuretexture": "MeasureTexture",
"medialaxis": "MedialAxis",
"medianfilter": "MedianFilter",
"mergeoutputfiles": "MergeOutputFiles",
"metadata": "Metadata",
"morph": "Morph",
"morphologicalskeleton": "MorphologicalSkeleton",
"namesandtypes": "NamesAndTypes",
"opening": "Opening",
"overlayobjects": "OverlayObjects",
"overlayoutlines": "OverlayOutlines",
"reducenoise": "ReduceNoise",
"relateobjects": "RelateObjects",
"removeholes": "RemoveHoles",
"rescaleintensity": "RescaleIntensity",
"resize": "Resize",
"resizeobjects": "ResizeObjects",
"savecroppedobjects": "SaveCroppedObjects",
"saveimages": "SaveImages",
"shrinktoobjectcenters": "ShrinkToObjectCenters",
"smooth": "Smooth",
"splitormergeobjects": "SplitOrMergeObjects",
"straightenworms": "StraightenWorms",
"matchtemplate": "MatchTemplate",
"threshold": "Threshold",
"trackobjects": "TrackObjects",
"tile": "Tile",
"unmixcolors": "UnmixColors",
"untangleworms": "UntangleWorms",
"watershed": "Watershed",
}
# the builtin CP modules that will be loaded from the cellprofiler.modules directory
builtin_modules = [
"align",
"calculatemath",
"calculatestatistics",
"classifyobjects",
"classify",
"classifypixelsunet",
"closing",
"colortogray",
"convertimagetoobjects",
"convertobjectstoimage",
"correctilluminationcalculate",
"correctilluminationapply",
"createbatchfiles",
"crop",
"definegrid",
"dilateimage",
"dilateobjects",
"displaydataonimage",
"displaydensityplot",
"displayhistogram",
"displayplatemap",
"displayscatterplot",
"editobjectsmanually",
"enhanceedges",
"enhanceorsuppressfeatures",
"erosion",
"expandorshrinkobjects",
"exporttodatabase",
"exporttospreadsheet",
"fillobjects",
"filterobjects",
"flagimage",
"flipandrotate",
"gaussianfilter",
"graytocolor",
"groups",
"identifydeadworms",
"identifyobjectsingrid",
"identifyobjectsmanually",
"identifyprimaryobjects",
"identifysecondaryobjects",
"identifytertiaryobjects",
"imagemath",
"images",
"invertforprinting",
"labelimages",
"loadimages",
"loadsingleimage",
"loaddata",
"makeprojection",
"maskimage",
"maskobjects",
"medialaxis",
"metadata",
"measurecolocalization",
"measuregranularity",
"measureimageareaoccupied",
"measureimageintensity",
"measureimageoverlap",
"measureimagequality",
"measureimageskeleton",
"measureobjectintensity",
"measureobjectoverlap",
"measureobjectsizeshape",
"measureobjectneighbors",
"measureobjectintensitydistribution",
"measureobjectskeleton",
"measuretexture",
"medianfilter",
"mergeoutputfiles",
"morph",
"morphologicalskeleton",
"namesandtypes",
"opening",
"overlayobjects",
"overlayoutlines",
"reducenoise",
"relateobjects",
"removeholes",
"rescaleintensity",
"resizeobjects",
"resize",
"savecroppedobjects",
"saveimages",
"shrinktoobjectcenters",
"smooth",
"splitormergeobjects",
"straightenworms",
"matchtemplate",
"threshold",
"trackobjects",
"tile",
"unmixcolors",
"untangleworms",
"watershed",
]
all_modules = {}
svn_revisions = {}
pymodules = []
badmodules = []
datatools = []
pure_datatools = {}
do_not_override = ["set_settings", "create_from_handles", "test_valid", "module_class"]
should_override = ["create_settings", "settings", "run"]
def check_module(module, name):
if hasattr(module, "do_not_check"):
return
assert (
name == module.module_name
), "Module %s should have module_name %s (is %s)" % (name, name, module.module_name)
for method_name in do_not_override:
assert getattr(module, method_name) == getattr(
cpm.Module, method_name
), "Module %s should not override method %s" % (name, method_name)
for method_name in should_override:
assert getattr(module, method_name) != getattr(
cpm.Module, method_name
), "Module %s should override method %s" % (name, method_name)
def find_cpmodule(m):
"""Returns the CPModule from within the loaded Python module
m - an imported module
returns the CPModule class
"""
for v, val in list(m.__dict__.items()):
if isinstance(val, type) and issubclass(val, cpm.Module):
return val
raise ValueError(
"Could not find cellprofiler.module.Module class in %s" % m.__file__
)
def fill_modules():
del pymodules[:]
del badmodules[:]
del datatools[:]
all_modules.clear()
svn_revisions.clear()
def add_module(mod, check_svn):
try:
m = __import__(mod, globals(), locals(), ["__all__"], 0)
cp_module = find_cpmodule(m)
name = cp_module.module_name
except Exception as e:
logger.warning("Could not load %s", mod, exc_info=True)
badmodules.append((mod, e))
return
try:
pymodules.append(m)
if name in all_modules:
logger.warning(
"Multiple definitions of module %s\n\told in %s\n\tnew in %s",
name,
sys.modules[all_modules[name].__module__].__file__,
m.__file__,
)
all_modules[name] = cp_module
check_module(cp_module, name)
# attempt to instantiate
if not hasattr(cp_module, "do_not_check"):
cp_module()
if hasattr(cp_module, "run_as_data_tool"):
datatools.append(name)
if check_svn and hasattr(m, "__version__"):
match = re.match("^\$Revision: ([0-9]+) \$$", m.__version__)
if match is not None:
svn_revisions[name] = match.groups()[0]
if not hasattr(all_modules[name], "settings"):
# No settings = pure data tool
pure_datatools[name] = all_modules[name]
del all_modules[name]
except Exception as e:
logger.warning("Failed to load %s", name, exc_info=True)
badmodules.append((mod, e))
if name in all_modules:
del all_modules[name]
del pymodules[-1]
for mod in builtin_modules:
add_module("cellprofiler.modules." + mod, True)
plugin_directory = get_plugin_directory()
if plugin_directory is not None:
old_path = sys.path
sys.path.insert(0, plugin_directory)
try:
for mod in plugin_list():
add_module(mod, False)
finally:
sys.path = old_path
datatools.sort()
if len(badmodules) > 0:
logger.warning(
"could not load these modules: %s", ",".join([x[0] for x in badmodules])
)
def add_module_for_tst(module_class):
all_modules[module_class.module_name] = module_class
fill_modules()
__all__ = [
"instantiate_module",
"get_module_names",
"reload_modules",
"add_module_for_tst",
"builtin_modules",
]
replaced_modules = {
"LoadImageDirectory": ["LoadImages", "LoadData"],
"GroupMovieFrames": ["LoadImages"],
"IdentifyPrimLoG": ["IdentifyPrimaryObjects"],
"FileNameMetadata": ["LoadImages"],
}
depricated_modules = ["CorrectIllumination_Calculate_kate", "SubtractBackground"]
unimplemented_modules = ["LabelImages", "Restart", "SplitOrSpliceMovie"]
def get_module_class(module_name):
module_class = module_name.split(".")[-1]
if module_class not in all_modules:
if module_class in pure_datatools:
return pure_datatools[module_class]
if module_class in unimplemented_modules:
raise ValueError(
(
"The %s module has not yet been implemented. "
"It will be available in a later version "
"of CellProfiler."
)
% module_class
)
if module_class in depricated_modules:
raise ValueError(
(
"The %s module has been deprecated and will "
"not be implemented in CellProfiler 2.0."
)
% module_class
)
if module_class in replaced_modules:
raise ValueError(
(
"The %s module no longer exists. You can find "
"similar functionality in: %s"
)
% (module_class, ", ".join(replaced_modules[module_class]))
)
raise ValueError("Could not find the %s module" % module_class)
return all_modules[module_class]
def instantiate_module(module_name):
module = get_module_class(module_name)()
if module_name in svn_revisions:
module.svn_version = svn_revisions[module_name]
return module
def get_module_names():
return list(all_modules.keys())
def get_data_tool_names():
return datatools
def reload_modules():
for m in pymodules:
try:
del sys.modules[m.__name__]
except:
pass
fill_modules()