scipion-em-xmipp 24.12.2__py3-none-any.whl → 25.6.0__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.
- {scipion_em_xmipp-24.12.2.dist-info → scipion_em_xmipp-25.6.0.dist-info}/METADATA +10 -10
- scipion_em_xmipp-25.6.0.dist-info/RECORD +255 -0
- {scipion_em_xmipp-24.12.2.dist-info → scipion_em_xmipp-25.6.0.dist-info}/WHEEL +1 -1
- {scipion_em_xmipp-24.12.2.dist-info → scipion_em_xmipp-25.6.0.dist-info}/entry_points.txt +0 -1
- xmipp3/__init__.py +106 -73
- xmipp3/base.py +2 -1
- xmipp3/convert/convert.py +1 -1
- xmipp3/legacy/protocols/protocol_angular_resolution_alignment.py +204 -0
- xmipp3/legacy/protocols/protocol_apply_deformation_zernike3d.py +113 -0
- xmipp3/legacy/protocols/protocol_classification_gpuCorr.py +821 -0
- xmipp3/legacy/protocols/protocol_classification_gpuCorr_full.py +1014 -0
- xmipp3/legacy/protocols/protocol_classification_gpuCorr_semi.py +462 -0
- xmipp3/legacy/protocols/protocol_classify_kmeans2d.py +285 -0
- xmipp3/legacy/protocols/protocol_deep_align.py +859 -0
- xmipp3/legacy/protocols/protocol_deep_denoising.py +425 -0
- xmipp3/legacy/protocols/protocol_kmeans_clustering.py +122 -0
- xmipp3/legacy/protocols/protocol_metaprotocol_create_output.py +146 -0
- xmipp3/legacy/protocols/protocol_metaprotocol_create_subset.py +96 -0
- xmipp3/legacy/protocols/protocol_metaprotocol_discrete_heterogeneity_scheduler.py +516 -0
- xmipp3/legacy/protocols/protocol_metaprotocol_golden_highres.py +663 -0
- xmipp3/legacy/protocols/protocol_mltomo.py +576 -0
- xmipp3/legacy/protocols/protocol_movie_average.py +202 -0
- xmipp3/legacy/protocols/protocol_movie_opticalflow.py +416 -0
- xmipp3/legacy/protocols/protocol_particle_boxsize.py +133 -0
- xmipp3/legacy/protocols/protocol_reconstruct_heterogeneous.py +1031 -0
- xmipp3/legacy/protocols/protocol_rotational_spectra.py +212 -0
- xmipp3/legacy/protocols/protocol_solid_angles.py +632 -0
- xmipp3/legacy/protocols/protocol_split_volume.py +112 -0
- xmipp3/legacy/protocols/protocol_split_volume_hierarchical_cluster.py +865 -0
- xmipp3/legacy/protocols/protocol_subtract_projection.py +265 -0
- xmipp3/legacy/tests/test_protocol_deep_denoising.py +64 -0
- xmipp3/legacy/tests/test_protocols_angular_resolution_alignment.py +88 -0
- xmipp3/legacy/tests/test_protocols_gpuCorr_classifier.py +141 -0
- xmipp3/legacy/tests/test_protocols_gpuCorr_fullStreaming.py +172 -0
- xmipp3/legacy/tests/test_protocols_gpuCorr_semiStreaming.py +68 -0
- xmipp3/legacy/tests/test_protocols_metaprotocol_golden_highres.py +96 -0
- xmipp3/legacy/tests/test_protocols_metaprotocol_heterogeneity.py +78 -0
- xmipp3/legacy/tests/test_protocols_mixed_movies.py +148 -0
- xmipp3/legacy/tests/test_protocols_solid_angles.py +161 -0
- xmipp3/legacy/tests/test_protocols_subtract_projection.py +595 -0
- xmipp3/legacy/tests/test_protocols_xmipp_movies.py +229 -0
- xmipp3/legacy/viewers/viewer_angular_resolution_alignment.py +148 -0
- xmipp3/legacy/viewers/viewer_deep_align.py +121 -0
- xmipp3/legacy/viewers/viewer_metaprotocol_golden_highres.py +140 -0
- xmipp3/legacy/viewers/viewer_mltomo.py +297 -0
- xmipp3/legacy/viewers/viewer_solid_angles.py +91 -0
- xmipp3/legacy/viewers/viewer_split_volume.py +57 -0
- xmipp3/protocols/__init__.py +1 -2
- xmipp3/protocols/protocol_align_volume.py +34 -8
- xmipp3/protocols/protocol_apply_alignment.py +1 -1
- xmipp3/protocols/protocol_apply_tilt_to_ctf.py +2 -3
- xmipp3/protocols/protocol_apply_zernike3d.py +2 -1
- xmipp3/protocols/protocol_assignment_tilt_pair.py +1 -2
- xmipp3/protocols/protocol_center_particles.py +1 -1
- xmipp3/protocols/protocol_cl2d.py +1 -2
- xmipp3/protocols/protocol_cl2d_align.py +1 -1
- xmipp3/protocols/protocol_cl2d_clustering.py +1 -1
- xmipp3/protocols/protocol_classify_pca.py +25 -9
- xmipp3/protocols/protocol_classify_pca_streaming.py +67 -42
- xmipp3/protocols/protocol_compute_likelihood.py +368 -0
- xmipp3/protocols/protocol_consensus_local_ctf.py +1 -1
- xmipp3/protocols/protocol_convert_pdb.py +9 -1
- xmipp3/protocols/protocol_create_gallery.py +1 -1
- xmipp3/protocols/protocol_ctf_consensus.py +14 -8
- xmipp3/protocols/protocol_ctf_correct_wiener2d.py +1 -1
- xmipp3/protocols/protocol_ctf_micrographs.py +1 -1
- xmipp3/protocols/protocol_deep_center_predict.py +10 -1
- xmipp3/protocols/protocol_deep_hand.py +19 -2
- xmipp3/protocols/protocol_deep_micrograph_screen.py +1 -1
- xmipp3/protocols/protocol_extract_asymmetric_unit.py +1 -1
- xmipp3/protocols/protocol_extract_particles.py +22 -10
- xmipp3/protocols/protocol_extract_particles_movies.py +1 -1
- xmipp3/protocols/protocol_extract_particles_pairs.py +1 -1
- xmipp3/protocols/protocol_flexalign.py +3 -4
- xmipp3/protocols/protocol_helical_parameters.py +1 -4
- xmipp3/protocols/protocol_movie_alignment_consensus.py +122 -40
- xmipp3/protocols/protocol_movie_dose_analysis.py +79 -82
- xmipp3/protocols/protocol_movie_max_shift.py +6 -2
- xmipp3/protocols/protocol_movie_split_frames.py +165 -88
- xmipp3/protocols/protocol_particle_pick_automatic.py +1 -2
- xmipp3/protocols/protocol_particle_pick_consensus.py +7 -0
- xmipp3/protocols/protocol_particle_pick_pairs.py +1 -1
- xmipp3/protocols/protocol_phantom_create.py +1 -1
- xmipp3/protocols/protocol_pick_noise.py +1 -1
- xmipp3/protocols/protocol_postProcessing_deepPostProcessing.py +5 -5
- xmipp3/protocols/protocol_preprocess/protocol_crop_resize.py +3 -2
- xmipp3/protocols/protocol_preprocess/protocol_filter.py +2 -2
- xmipp3/protocols/protocol_preprocess/protocol_image_operate.py +2 -2
- xmipp3/protocols/protocol_preprocess/protocol_mask.py +1 -1
- xmipp3/protocols/protocol_preprocess/protocol_preprocess.py +6 -4
- xmipp3/protocols/protocol_preprocess/protocol_process.py +20 -7
- xmipp3/protocols/protocol_preprocess_micrographs.py +3 -2
- xmipp3/protocols/protocol_projmatch/projmatch_steps.py +1 -1
- xmipp3/protocols/protocol_projmatch/protocol_projmatch.py +1 -1
- xmipp3/protocols/protocol_random_conical_tilt.py +1 -2
- xmipp3/protocols/protocol_ransac.py +41 -37
- xmipp3/protocols/protocol_reconstruct_fourier.py +5 -1
- xmipp3/protocols/protocol_reconstruct_highres.py +13 -7
- xmipp3/protocols/protocol_reconstruct_significant.py +14 -21
- xmipp3/protocols/protocol_resolution3d.py +1 -1
- xmipp3/protocols/protocol_resolution_deepres.py +1 -1
- xmipp3/protocols/protocol_resolution_directional.py +1 -1
- xmipp3/protocols/protocol_resolution_fso.py +1 -1
- xmipp3/protocols/protocol_resolution_monogenic_signal.py +19 -4
- xmipp3/protocols/protocol_rotate_volume.py +1 -1
- xmipp3/protocols/protocol_screen_deepConsensus.py +25 -5
- xmipp3/protocols/protocol_shift_volume.py +1 -1
- xmipp3/protocols/protocol_simulate_ctf.py +5 -3
- xmipp3/protocols/protocol_structure_map.py +9 -1
- xmipp3/protocols/protocol_subtract_projection.py +25 -10
- xmipp3/protocols/protocol_tilt_analysis.py +3 -3
- xmipp3/protocols/protocol_validate_fscq.py +2 -2
- xmipp3/protocols/protocol_validate_nontilt.py +1 -1
- xmipp3/protocols/protocol_volume_adjust_sub.py +8 -4
- xmipp3/protocols/protocol_volume_deform_zernike3d.py +1 -1
- xmipp3/protocols/protocol_volume_local_sharpening.py +1 -1
- xmipp3/protocols/protocol_volume_strain.py +1 -1
- xmipp3/protocols.conf +1 -3
- xmipp3/tests/test_protocol_likelihood.py +240 -0
- xmipp3/tests/test_protocol_split_frames.py +136 -0
- xmipp3/tests/test_protocol_validate_fscq.py +2 -2
- xmipp3/tests/test_protocols_highres.py +44 -5
- xmipp3/tests/test_protocols_xmipp_2d.py +1 -48
- xmipp3/tests/test_protocols_xmipp_mics.py +20 -16
- xmipp3/tests/test_protocols_xmipp_movies.py +4 -4
- xmipp3/version.py +5 -5
- xmipp3/viewers/__init__.py +1 -1
- xmipp3/viewers/viewer_LL_matrix.py +273 -0
- xmipp3/viewers/viewer_deep_consensus.py +1 -7
- xmipp3/viewers/viewer_dose_analysis.py +18 -2
- xmipp3/viewers/viewer_resolution_fso.py +18 -6
- xmipp3/viewers/viewer_structure_map.py +53 -11
- xmipp3/wizards.py +3 -2
- xmipp3/xmipp_logo_devel.png +0 -0
- scipion_em_xmipp-24.12.2.dist-info/RECORD +0 -210
- {scipion_em_xmipp-24.12.2.dist-info → scipion_em_xmipp-25.6.0.dist-info}/LICENSE +0 -0
- {scipion_em_xmipp-24.12.2.dist-info → scipion_em_xmipp-25.6.0.dist-info}/top_level.txt +0 -0
- /xmipp3/{protocols → legacy/protocols}/protocol_enrich.py +0 -0
- /xmipp3/{protocols → legacy/protocols}/protocol_screen_deeplearning.py +0 -0
|
@@ -0,0 +1,859 @@
|
|
|
1
|
+
# **************************************************************************
|
|
2
|
+
# *
|
|
3
|
+
# * Authors: Amaya Jimenez
|
|
4
|
+
# *
|
|
5
|
+
# * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
|
|
6
|
+
# *
|
|
7
|
+
# * This program is free software; you can redistribute it and/or modify
|
|
8
|
+
# * it under the terms of the GNU General Public License as published by
|
|
9
|
+
# * the Free Software Foundation; either version 2 of the License, or
|
|
10
|
+
# * (at your option) any later version.
|
|
11
|
+
# *
|
|
12
|
+
# * This program is distributed in the hope that it will be useful,
|
|
13
|
+
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
# * GNU General Public License for more details.
|
|
16
|
+
# *
|
|
17
|
+
# * You should have received a copy of the GNU General Public License
|
|
18
|
+
# * along with this program; if not, write to the Free Software
|
|
19
|
+
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
20
|
+
# * 02111-1307 USA
|
|
21
|
+
# *
|
|
22
|
+
# * All comments concerning this program package may be sent to the
|
|
23
|
+
# * e-mail address 'scipion@cnb.csic.es'
|
|
24
|
+
# *
|
|
25
|
+
# **************************************************************************
|
|
26
|
+
|
|
27
|
+
from pyworkflow import VERSION_3_0
|
|
28
|
+
from pyworkflow.protocol import STEPS_PARALLEL
|
|
29
|
+
from pyworkflow.protocol.params import (PointerParam, StringParam, FloatParam,
|
|
30
|
+
IntParam, BooleanParam, GPU_LIST)
|
|
31
|
+
from pyworkflow.protocol.constants import LEVEL_ADVANCED
|
|
32
|
+
from pyworkflow.utils.path import moveFile, cleanPattern
|
|
33
|
+
from pwem.protocols import ProtRefine3D
|
|
34
|
+
from xmipp3.base import writeInfoField, readInfoField
|
|
35
|
+
from pwem.emlib.metadata import iterRows, getFirstRow
|
|
36
|
+
import pwem.emlib.metadata as md
|
|
37
|
+
from xmipp3.convert import createItemMatrix, setXmippAttributes, readSetOfParticles
|
|
38
|
+
from pwem import ALIGN_PROJ
|
|
39
|
+
from pwem import emlib
|
|
40
|
+
from pwem.emlib.image import ImageHandler
|
|
41
|
+
import os
|
|
42
|
+
import sys
|
|
43
|
+
import numpy as np
|
|
44
|
+
import math
|
|
45
|
+
from shutil import copy
|
|
46
|
+
from os import remove
|
|
47
|
+
from os.path import exists, join
|
|
48
|
+
import xmipp3
|
|
49
|
+
|
|
50
|
+
class XmippProtDeepAlign(ProtRefine3D, xmipp3.XmippProtocol):
|
|
51
|
+
"""Performs an angular assignment using deep learning"""
|
|
52
|
+
_label = 'deep align'
|
|
53
|
+
_lastUpdateVersion = VERSION_3_0
|
|
54
|
+
_conda_env = 'xmipp_DLTK_v0.3'
|
|
55
|
+
_ih = ImageHandler()
|
|
56
|
+
_cond_modelPretrainTrue = 'modelPretrain==True'
|
|
57
|
+
_cond_modelPretrainFalse = 'modelPretrain==False'
|
|
58
|
+
_fnCorrectedParticlesStk = 'corrected_particles.stk'
|
|
59
|
+
_fnCorrectedParticlesXmd = 'corrected_particles.xmd'
|
|
60
|
+
_fnVolumeVol = 'volume.vol'
|
|
61
|
+
_fnConeCenterDoc = 'coneCenters.doc'
|
|
62
|
+
_tempNumXmd = '%d.xmd'
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def __init__(self, **args):
|
|
66
|
+
ProtRefine3D.__init__(self, **args)
|
|
67
|
+
self.stepsExecutionMode = STEPS_PARALLEL
|
|
68
|
+
|
|
69
|
+
# --------------------------- DEFINE param functions --------------------------------------------
|
|
70
|
+
def _defineParams(self, form):
|
|
71
|
+
form.addHidden(GPU_LIST, StringParam, default='0',
|
|
72
|
+
expertLevel=LEVEL_ADVANCED,
|
|
73
|
+
label="Choose GPU IDs",
|
|
74
|
+
help="GPU may have several cores. Set it to zero"
|
|
75
|
+
" if you do not know what we are talking about."
|
|
76
|
+
" First core index is 0, second 1 and so on.")
|
|
77
|
+
form.addSection(label='Input')
|
|
78
|
+
form.addParam('inputSet', PointerParam, label="Input images",
|
|
79
|
+
pointerClass='SetOfParticles')
|
|
80
|
+
form.addParam('inputVolume', PointerParam, label="Volume",
|
|
81
|
+
pointerClass='Volume')
|
|
82
|
+
|
|
83
|
+
form.addParam('modelPretrain', BooleanParam, default=False,
|
|
84
|
+
label='Choose your if you want to use pretrained models',
|
|
85
|
+
help='Setting "yes" you can choose previously trained models. '
|
|
86
|
+
'If you choose "no" new models will be trained.')
|
|
87
|
+
form.addParam('pretrainedModels', PointerParam,
|
|
88
|
+
pointerClass=self.getClassName(),
|
|
89
|
+
condition=self._cond_modelPretrainTrue,
|
|
90
|
+
label='Set pretrained models',
|
|
91
|
+
help='Choose the protocol where your models were trained. '
|
|
92
|
+
'Be careful with using proper models for your new prediction.')
|
|
93
|
+
|
|
94
|
+
form.addParam('inputTrainSet', PointerParam, label="Input training set",
|
|
95
|
+
pointerClass='SetOfParticles',
|
|
96
|
+
pointerCondition='hasAlignmentProj',
|
|
97
|
+
help='The set of particles previously aligned to be used as training set',
|
|
98
|
+
condition=self._cond_modelPretrainFalse)
|
|
99
|
+
form.addParam('targetResolution', FloatParam, label="Target resolution",
|
|
100
|
+
default=3.0,
|
|
101
|
+
help="In Angstroms, the images and the volume are rescaled so that this resolution is at "
|
|
102
|
+
"2/3 of the Fourier spectrum.")
|
|
103
|
+
form.addParam('symmetryGroup', StringParam, default="c1",
|
|
104
|
+
label='Symmetry group',
|
|
105
|
+
help='If no symmetry is present, give c1')
|
|
106
|
+
form.addParam('numEpochs', IntParam,
|
|
107
|
+
label="Number of epochs for training",
|
|
108
|
+
default=10,
|
|
109
|
+
help="Number of epochs for training.",
|
|
110
|
+
condition=self._cond_modelPretrainFalse)
|
|
111
|
+
form.addParam('batchSize', IntParam,
|
|
112
|
+
label="Batch size for training",
|
|
113
|
+
default=128,
|
|
114
|
+
help="Batch size for training.",
|
|
115
|
+
condition=self._cond_modelPretrainFalse)
|
|
116
|
+
form.addParam('spanConesTilt', FloatParam,
|
|
117
|
+
label="Distance between region centers",
|
|
118
|
+
default=30,
|
|
119
|
+
help="Distance in degrees between region centers.",
|
|
120
|
+
condition=self._cond_modelPretrainFalse)
|
|
121
|
+
form.addParam('numConesSelected', IntParam,
|
|
122
|
+
label="Number of selected regions per image",
|
|
123
|
+
default=2,
|
|
124
|
+
help="Number of selected regions per image.")
|
|
125
|
+
form.addParam('applyCTF', BooleanParam, default=False,
|
|
126
|
+
label='Correct CTF',
|
|
127
|
+
help='Setting "yes" a wiener filter will be applied to correct the ctf in the input particles. ')
|
|
128
|
+
form.addParam('gpuAlign', BooleanParam, label="Use GPU alignment", default=True,
|
|
129
|
+
help='Use GPU alignment algorithm to determine the final 3D alignment parameters')
|
|
130
|
+
form.addParam('myMPI', IntParam, label="Xmipp MPIs", default=8,
|
|
131
|
+
help='Number of MPI to run the Xmipp programs to prepare the input images sets.')
|
|
132
|
+
|
|
133
|
+
form.addParallelSection(threads=8, mpi=1)
|
|
134
|
+
|
|
135
|
+
# --------------------------- INSERT steps functions --------------------------------------------
|
|
136
|
+
def _insertAllSteps(self):
|
|
137
|
+
|
|
138
|
+
deps = []
|
|
139
|
+
deps2 = []
|
|
140
|
+
|
|
141
|
+
self.lastIter = 0
|
|
142
|
+
self.imgsFn = self._getExtraPath('input_imgs.xmd')
|
|
143
|
+
self.trainImgsFn = self._getExtraPath('train_input_imgs.xmd')
|
|
144
|
+
|
|
145
|
+
firstStepId = self._insertFunctionStep("convertStep")
|
|
146
|
+
firstStepId = self._insertFunctionStep("computeTrainingSet", 'projections', prerequisites=[firstStepId])
|
|
147
|
+
|
|
148
|
+
# Trainig steps
|
|
149
|
+
firstStepId = self._insertFunctionStep("prepareImagesForTraining", prerequisites=[firstStepId])
|
|
150
|
+
|
|
151
|
+
if self.useQueueForSteps() or self.useQueue():
|
|
152
|
+
myStr = os.environ["CUDA_VISIBLE_DEVICES"]
|
|
153
|
+
else:
|
|
154
|
+
myStr = self.gpuList.get()
|
|
155
|
+
os.environ["CUDA_VISIBLE_DEVICES"] = self.gpuList.get()
|
|
156
|
+
|
|
157
|
+
numGPU = myStr.split(',')
|
|
158
|
+
for idx, gpuId in enumerate(numGPU):
|
|
159
|
+
stepId = self._insertFunctionStep("trainNClassifiers2ClassesStep", idx, gpuId, len(numGPU),
|
|
160
|
+
prerequisites=[firstStepId])
|
|
161
|
+
deps.append(stepId)
|
|
162
|
+
|
|
163
|
+
# Predict step
|
|
164
|
+
predictStepId = self._insertFunctionStep("predictStep", numGPU[0], prerequisites=deps)
|
|
165
|
+
|
|
166
|
+
# Correlation step
|
|
167
|
+
if self.gpuAlign:
|
|
168
|
+
for idx, gpuId in enumerate(numGPU):
|
|
169
|
+
stepId = self._insertFunctionStep("correlationCudaStep", idx, str(idx), len(numGPU),
|
|
170
|
+
prerequisites=[predictStepId])
|
|
171
|
+
deps2.append(stepId)
|
|
172
|
+
else:
|
|
173
|
+
stepId = self._insertFunctionStep("correlationSignificantStep", prerequisites=[predictStepId])
|
|
174
|
+
deps2.append(stepId)
|
|
175
|
+
|
|
176
|
+
stepId = self._insertFunctionStep("createOutputMetadataStep", prerequisites=deps2)
|
|
177
|
+
|
|
178
|
+
self._insertFunctionStep("createOutputStep", prerequisites=[stepId])
|
|
179
|
+
|
|
180
|
+
# --------------------------- STEPS functions ---------------------------------------------------
|
|
181
|
+
|
|
182
|
+
def _getProjectionsExp(self, num):
|
|
183
|
+
return self._getExtraPath('projectionsExp%d.xmd' % num)
|
|
184
|
+
|
|
185
|
+
def _getConePrediction(self):
|
|
186
|
+
return self._getExtraPath('conePrediction.txt')
|
|
187
|
+
|
|
188
|
+
def _getOutCone(self, num):
|
|
189
|
+
return 'outCone%d.xmd' % num
|
|
190
|
+
|
|
191
|
+
def _resize(self, Xdim, fnCorrected, prefix, fnTarget):
|
|
192
|
+
if self.newXdim != Xdim:
|
|
193
|
+
self.runJob("xmipp_image_resize",
|
|
194
|
+
"-i %s -o %s --save_metadata_stack %s --fourier %d" %
|
|
195
|
+
(fnCorrected,
|
|
196
|
+
self._getExtraPath(prefix + '.stk'),
|
|
197
|
+
self._getExtraPath(prefix + '.xmd'),
|
|
198
|
+
self.newXdim), numberOfMpi=self.myMPI.get())
|
|
199
|
+
moveFile(self._getExtraPath(prefix + '.xmd'), fnTarget)
|
|
200
|
+
|
|
201
|
+
def _correctWiener(self, hasCTF, fn, Ts):
|
|
202
|
+
if hasCTF and self.applyCTF.get():
|
|
203
|
+
fnCorrectedStk = self._getExtraPath(self._fnCorrectedParticlesStk)
|
|
204
|
+
fnCorrected = self._getExtraPath(self._fnCorrectedParticlesXmd)
|
|
205
|
+
args = "-i %s -o %s --save_metadata_stack %s --keep_input_columns" % (
|
|
206
|
+
fn, fnCorrectedStk, fnCorrected)
|
|
207
|
+
args += " --sampling_rate %f --correct_envelope" % Ts
|
|
208
|
+
if self.inputSet.get().isPhaseFlipped():
|
|
209
|
+
args += " --phase_flipped"
|
|
210
|
+
self.runJob("xmipp_ctf_correct_wiener2d",
|
|
211
|
+
args, numberOfMpi=self.myMPI.get())
|
|
212
|
+
|
|
213
|
+
def _removeCorrectedParticles(self):
|
|
214
|
+
if exists(self._getExtraPath(self._fnCorrectedParticlesStk)):
|
|
215
|
+
remove(self._getExtraPath(self._fnCorrectedParticlesStk))
|
|
216
|
+
remove(self._getExtraPath(self._fnCorrectedParticlesXmd))
|
|
217
|
+
|
|
218
|
+
def convertStep(self):
|
|
219
|
+
if self.modelPretrain.get() is True:
|
|
220
|
+
fnPreProtocol = self.pretrainedModels.get()._getExtraPath()
|
|
221
|
+
preXDim = readInfoField(fnPreProtocol, "size", emlib.MDL_XSIZE)
|
|
222
|
+
self.inputTrainSet = self.pretrainedModels.get().inputTrainSet
|
|
223
|
+
|
|
224
|
+
from ..convert import writeSetOfParticles
|
|
225
|
+
inputParticles = self.inputSet.get()
|
|
226
|
+
writeSetOfParticles(inputParticles, self.imgsFn)
|
|
227
|
+
|
|
228
|
+
Ts = inputParticles.getSamplingRate()
|
|
229
|
+
row = getFirstRow(self.imgsFn)
|
|
230
|
+
hasCTF = row.containsLabel(emlib.MDL_CTF_DEFOCUSU) or emlib.containsLabel(
|
|
231
|
+
emlib.MDL_CTF_MODEL)
|
|
232
|
+
|
|
233
|
+
fnCorrected = self.imgsFn
|
|
234
|
+
|
|
235
|
+
self._correctWiener(hasCTF, self.imgsFn, Ts)
|
|
236
|
+
|
|
237
|
+
Xdim = inputParticles.getXDim()
|
|
238
|
+
newTs = self.targetResolution.get() * 1.0 / 3.0
|
|
239
|
+
newTs = max(Ts, newTs)
|
|
240
|
+
|
|
241
|
+
self.newXdim = int(preXDim) if self.modelPretrain.get() else int(
|
|
242
|
+
float(Xdim * Ts / newTs))
|
|
243
|
+
|
|
244
|
+
self.firstMaxShift = int(round(self.newXdim / 10))
|
|
245
|
+
writeInfoField(self._getExtraPath(), "sampling",
|
|
246
|
+
emlib.MDL_SAMPLINGRATE, newTs)
|
|
247
|
+
writeInfoField(self._getExtraPath(), "size", emlib.MDL_XSIZE,
|
|
248
|
+
self.newXdim)
|
|
249
|
+
|
|
250
|
+
self._resize(Xdim, fnCorrected, 'scaled_particles', self.imgsFn)
|
|
251
|
+
|
|
252
|
+
self._removeCorrectedParticles()
|
|
253
|
+
|
|
254
|
+
fnVol = self._getTmpPath(self._fnVolumeVol)
|
|
255
|
+
self._ih.convert(self.inputVolume.get(), fnVol)
|
|
256
|
+
Xdim = self.inputVolume.get().getDim()[0]
|
|
257
|
+
if Xdim != self.newXdim:
|
|
258
|
+
self.runJob("xmipp_image_resize",
|
|
259
|
+
"-i %s --fourier %d" % (fnVol, self.newXdim),
|
|
260
|
+
numberOfMpi=self.myMPI.get())
|
|
261
|
+
|
|
262
|
+
inputTrain = self.inputTrainSet.get()
|
|
263
|
+
writeSetOfParticles(inputTrain, self.trainImgsFn)
|
|
264
|
+
row = getFirstRow(self.trainImgsFn)
|
|
265
|
+
hasCTF = row.containsLabel(emlib.MDL_CTF_DEFOCUSU) or emlib.containsLabel(
|
|
266
|
+
emlib.MDL_CTF_MODEL)
|
|
267
|
+
|
|
268
|
+
fnCorrected = self.trainImgsFn
|
|
269
|
+
|
|
270
|
+
self._correctWiener(hasCTF, self.trainImgsFn, Ts)
|
|
271
|
+
|
|
272
|
+
self._resize(Xdim, fnCorrected,
|
|
273
|
+
'scaled_train_particles', self.trainImgsFn)
|
|
274
|
+
|
|
275
|
+
self._removeCorrectedParticles()
|
|
276
|
+
|
|
277
|
+
def generateConeCenters(self, fn):
|
|
278
|
+
fnVol = self._getTmpPath(self._fnVolumeVol)
|
|
279
|
+
fnCenters = self._getExtraPath(fn + ".stk")
|
|
280
|
+
fnCentersMd = self._getExtraPath(fn + ".doc")
|
|
281
|
+
# if self.spanConesTilt.get()>30:
|
|
282
|
+
# sampling_rate = 30
|
|
283
|
+
# else:
|
|
284
|
+
# sampling_rate = self.spanConesTilt.get()
|
|
285
|
+
|
|
286
|
+
self.runJob("xmipp_angular_project_library",
|
|
287
|
+
"-i %s -o %s --sym c1 --sampling_rate %d"
|
|
288
|
+
% (fnVol, fnCenters, self.spanConesTilt.get()),
|
|
289
|
+
numberOfMpi=self.myMPI.get())
|
|
290
|
+
mdExp = emlib.MetaData(fnCentersMd)
|
|
291
|
+
return mdExp.size()
|
|
292
|
+
|
|
293
|
+
def angularDistance(self, rot, tilt, mdCones):
|
|
294
|
+
# Angular distance between particle and region center
|
|
295
|
+
dist = []
|
|
296
|
+
for row in iterRows(mdCones):
|
|
297
|
+
rotCenterCone = row.getValue(emlib.MDL_ANGLE_ROT)
|
|
298
|
+
tiltCenterCone = row.getValue(emlib.MDL_ANGLE_TILT)
|
|
299
|
+
if rotCenterCone < 0:
|
|
300
|
+
rotCenterCone = rotCenterCone + 360
|
|
301
|
+
if tiltCenterCone < 0:
|
|
302
|
+
tiltCenterCone = tiltCenterCone + 360
|
|
303
|
+
srot = math.sin(math.radians(rot))
|
|
304
|
+
crot = math.cos(math.radians(rot))
|
|
305
|
+
stilt = math.sin(math.radians(tilt))
|
|
306
|
+
ctilt = math.cos(math.radians(tilt))
|
|
307
|
+
srotCone = math.sin(math.radians(rotCenterCone))
|
|
308
|
+
crotCone = math.cos(math.radians(rotCenterCone))
|
|
309
|
+
stiltCone = math.sin(math.radians(tiltCenterCone))
|
|
310
|
+
ctiltCone = math.cos(math.radians(tiltCenterCone))
|
|
311
|
+
aux = (stilt * crot * stiltCone * crotCone) + (
|
|
312
|
+
stilt * srot * stiltCone * srotCone) + (
|
|
313
|
+
ctilt * ctiltCone)
|
|
314
|
+
auxA = aux
|
|
315
|
+
if aux < -1:
|
|
316
|
+
auxA = -1
|
|
317
|
+
elif aux > 1:
|
|
318
|
+
auxA = 1
|
|
319
|
+
auxAcos = math.degrees(math.acos(auxA))
|
|
320
|
+
dist.append(auxAcos)
|
|
321
|
+
minDist = min(dist)
|
|
322
|
+
finalCone = dist.index(minDist) + 1
|
|
323
|
+
return finalCone
|
|
324
|
+
|
|
325
|
+
def computeTrainingSet(self, nameTrain):
|
|
326
|
+
|
|
327
|
+
totalCones = self.generateConeCenters('coneCenters')
|
|
328
|
+
self.numCones = totalCones
|
|
329
|
+
fnCentersMd = self._getExtraPath(self._fnConeCenterDoc)
|
|
330
|
+
mdCones = emlib.MetaData(fnCentersMd)
|
|
331
|
+
|
|
332
|
+
auxList = []
|
|
333
|
+
|
|
334
|
+
mdTrain = emlib.MetaData(self.trainImgsFn)
|
|
335
|
+
mdList = []
|
|
336
|
+
for i in range(totalCones):
|
|
337
|
+
mdList.append(emlib.MetaData())
|
|
338
|
+
for row in iterRows(mdTrain):
|
|
339
|
+
rot = row.getValue(emlib.MDL_ANGLE_ROT)
|
|
340
|
+
tilt = row.getValue(emlib.MDL_ANGLE_TILT)
|
|
341
|
+
flip = row.getValue(emlib.MDL_FLIP)
|
|
342
|
+
if flip:
|
|
343
|
+
tilt = tilt + 180
|
|
344
|
+
if rot < 0:
|
|
345
|
+
rot = rot + 360
|
|
346
|
+
if tilt < 0:
|
|
347
|
+
tilt = tilt + 360
|
|
348
|
+
numCone = self.angularDistance(rot, tilt, mdCones)
|
|
349
|
+
mdCone = mdList[numCone - 1]
|
|
350
|
+
auxList.append(numCone - 1)
|
|
351
|
+
row.addToMd(mdCone)
|
|
352
|
+
for i in range(totalCones):
|
|
353
|
+
fnTrain = self._getExtraPath(nameTrain + self._tempNumXmd % (i + 1))
|
|
354
|
+
mdList[i].write(fnTrain)
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def prepareImagesForTraining(self):
|
|
358
|
+
|
|
359
|
+
fnCentersMd = self._getExtraPath(self._fnConeCenterDoc)
|
|
360
|
+
mdCones = emlib.MetaData(fnCentersMd)
|
|
361
|
+
span = self.spanConesTilt.get()
|
|
362
|
+
counterCones = 0
|
|
363
|
+
for row in iterRows(mdCones):
|
|
364
|
+
rotCenter = row.getValue(emlib.MDL_ANGLE_ROT)
|
|
365
|
+
tiltCenter = row.getValue(emlib.MDL_ANGLE_TILT)
|
|
366
|
+
if rotCenter < 0:
|
|
367
|
+
rotCenter = rotCenter + 360
|
|
368
|
+
if tiltCenter < 0:
|
|
369
|
+
tiltCenter = tiltCenter + 360
|
|
370
|
+
iniRot = rotCenter - span
|
|
371
|
+
endRot = rotCenter + span
|
|
372
|
+
iniTilt = tiltCenter - span
|
|
373
|
+
endTilt = tiltCenter + span
|
|
374
|
+
if iniRot < 0:
|
|
375
|
+
iniRot = iniRot + 360
|
|
376
|
+
if iniTilt < 0:
|
|
377
|
+
iniTilt = iniTilt + 360
|
|
378
|
+
if endRot < 0:
|
|
379
|
+
endRot = endRot + 360
|
|
380
|
+
if endTilt < 0:
|
|
381
|
+
endTilt = endTilt + 360
|
|
382
|
+
mdProj = emlib.MetaData(self._getExtraPath('projections%d.xmd' % (counterCones + 1)))
|
|
383
|
+
sizeProj = mdProj.size()
|
|
384
|
+
if sizeProj > 0:
|
|
385
|
+
lastLabel = counterCones + 1
|
|
386
|
+
self.projectStep(300, iniRot, endRot, iniTilt, endTilt,
|
|
387
|
+
'projectionsCudaCorr', counterCones + 1)
|
|
388
|
+
if self.modelPretrain.get() is False:
|
|
389
|
+
self.generateExpImagesStep(10000, 'projections',
|
|
390
|
+
'projectionsExp',
|
|
391
|
+
counterCones + 1)
|
|
392
|
+
else:
|
|
393
|
+
remove(self._getExtraPath('projections%d.xmd' % (counterCones + 1)))
|
|
394
|
+
counterCones = counterCones + 1
|
|
395
|
+
|
|
396
|
+
if self.modelPretrain.get() is False:
|
|
397
|
+
fnToFilter = self._getProjectionsExp(lastLabel)
|
|
398
|
+
self.runJob("xmipp_transform_filter", " -i %s --fourier low_pass %f" %
|
|
399
|
+
(fnToFilter, 0.15), numberOfMpi=self.myMPI.get())
|
|
400
|
+
|
|
401
|
+
def projectStep(self, numProj, iniRot, endRot, iniTilt, endTilt, fn, idx):
|
|
402
|
+
|
|
403
|
+
newXdim = readInfoField(self._getExtraPath(), "size",
|
|
404
|
+
emlib.MDL_XSIZE)
|
|
405
|
+
fnVol = self._getTmpPath(self._fnVolumeVol)
|
|
406
|
+
|
|
407
|
+
uniformProjectionsStr = """
|
|
408
|
+
# XMIPP_STAR_1 *
|
|
409
|
+
data_block1
|
|
410
|
+
_dimensions2D '%d %d'
|
|
411
|
+
_projRotRange '%d %d %d'
|
|
412
|
+
_projRotRandomness random
|
|
413
|
+
_projRotNoise '0'
|
|
414
|
+
_projTiltRange '%d %d 1'
|
|
415
|
+
_projTiltRandomness random
|
|
416
|
+
_projTiltNoise '0'
|
|
417
|
+
_projPsiRange '0 0 1'
|
|
418
|
+
_projPsiRandomness random
|
|
419
|
+
_projPsiNoise '0'
|
|
420
|
+
_noisePixelLevel '0'
|
|
421
|
+
_noiseCoord '0'
|
|
422
|
+
""" % (newXdim, newXdim, iniRot, endRot, numProj, iniTilt, endTilt)
|
|
423
|
+
fnParams = self._getExtraPath("uniformProjections%d.xmd" % idx)
|
|
424
|
+
fh = open(fnParams, "w")
|
|
425
|
+
fh.write(uniformProjectionsStr)
|
|
426
|
+
fh.close()
|
|
427
|
+
|
|
428
|
+
fnProjs = self._getExtraPath(fn + "%d.stk" % idx)
|
|
429
|
+
self.runJob("xmipp_phantom_project",
|
|
430
|
+
"-i %s -o %s --method fourier 1 0.5 "
|
|
431
|
+
"--params %s" % (fnVol, fnProjs, fnParams), numberOfMpi=1)
|
|
432
|
+
|
|
433
|
+
fnProjsXmd=fnProjs[:-3]+'xmd'
|
|
434
|
+
self.runJob("xmipp_metadata_utilities",
|
|
435
|
+
"-i %s --fill ref lineal 1 1 " % (fnProjsXmd), numberOfMpi=1)
|
|
436
|
+
|
|
437
|
+
cleanPattern(self._getExtraPath('uniformProjections*'))
|
|
438
|
+
|
|
439
|
+
def generateExpImagesStep(self, Nimgs, nameProj, nameExp, label):
|
|
440
|
+
fnProj = self._getExtraPath(nameProj + self._tempNumXmd % label)
|
|
441
|
+
fnExp = self._getExtraPath(nameExp + self._tempNumXmd % label)
|
|
442
|
+
fnLabels = self._getExtraPath('labels.txt')
|
|
443
|
+
mdIn = emlib.MetaData(fnProj)
|
|
444
|
+
mdExp = emlib.MetaData()
|
|
445
|
+
NimgsMd = mdIn.size()
|
|
446
|
+
Nrepeats = int(Nimgs / NimgsMd)
|
|
447
|
+
# if Nrepeats<10:
|
|
448
|
+
# Nrepeats=10
|
|
449
|
+
print("Nrepeats", Nrepeats)
|
|
450
|
+
if (label == 1 and exists(fnLabels)):
|
|
451
|
+
remove(fnLabels)
|
|
452
|
+
fileLabels = open(fnLabels, "a")
|
|
453
|
+
self._processRows(label, fnExp, mdIn, mdExp, Nrepeats, fileLabels)
|
|
454
|
+
mdExp.write(fnExp)
|
|
455
|
+
fileLabels.close()
|
|
456
|
+
if (label - 1) > 0:
|
|
457
|
+
labelPrev = -1
|
|
458
|
+
for n in range(1, label):
|
|
459
|
+
if exists(self._getExtraPath(nameExp + self._tempNumXmd % (label - n))):
|
|
460
|
+
labelPrev = label - n
|
|
461
|
+
break
|
|
462
|
+
if labelPrev != -1:
|
|
463
|
+
lastFnExp = self._getExtraPath(
|
|
464
|
+
nameExp + self._tempNumXmd % (labelPrev))
|
|
465
|
+
self.runJob("xmipp_metadata_utilities",
|
|
466
|
+
" -i %s --set union %s -o %s " %
|
|
467
|
+
(lastFnExp, fnExp, fnExp), numberOfMpi=1)
|
|
468
|
+
remove(fnProj)
|
|
469
|
+
|
|
470
|
+
def _processRows(self, label, fnExp, mdIn, mdExp, Nrepeats, fileLabels):
|
|
471
|
+
newXdim = readInfoField(self._getExtraPath(), "size",
|
|
472
|
+
emlib.MDL_XSIZE)
|
|
473
|
+
maxPsi = 180
|
|
474
|
+
maxShift = round(newXdim / 10)
|
|
475
|
+
for row in iterRows(mdIn):
|
|
476
|
+
fnImg = row.getValue(emlib.MDL_IMAGE)
|
|
477
|
+
myRow = row
|
|
478
|
+
I = emlib.Image(fnImg)
|
|
479
|
+
Xdim, Ydim, _, _ = I.getDimensions()
|
|
480
|
+
Xdim2 = Xdim / 2
|
|
481
|
+
Ydim2 = Ydim / 2
|
|
482
|
+
if Nrepeats == 0:
|
|
483
|
+
myRow.addToMd(mdExp)
|
|
484
|
+
idx += 1
|
|
485
|
+
fileLabels.write(str(label - 1) + '\n')
|
|
486
|
+
else:
|
|
487
|
+
for i in range(Nrepeats):
|
|
488
|
+
psiDeg = np.random.uniform(-maxPsi, maxPsi)
|
|
489
|
+
psi = psiDeg * math.pi / 180.0
|
|
490
|
+
deltaX = np.random.uniform(-maxShift, maxShift)
|
|
491
|
+
deltaY = np.random.uniform(-maxShift, maxShift)
|
|
492
|
+
c = math.cos(psi)
|
|
493
|
+
s = math.sin(psi)
|
|
494
|
+
M = np.float32([[c, s, (1 - c) * Xdim2 - s * Ydim2 + deltaX],
|
|
495
|
+
[-s, c, s * Xdim2 + (1 - c) * Ydim2 + deltaY]])
|
|
496
|
+
newFn = ('%06d@' % idx) + fnExp[:-3] + 'stk'
|
|
497
|
+
self._ih.applyTransform(
|
|
498
|
+
fnImg, newFn, M, (Ydim, Xdim), doWrap=True)
|
|
499
|
+
|
|
500
|
+
myRow.setValue(emlib.MDL_IMAGE, newFn)
|
|
501
|
+
myRow.setValue(emlib.MDL_ANGLE_PSI, psiDeg)
|
|
502
|
+
myRow.setValue(emlib.MDL_SHIFT_X, deltaX)
|
|
503
|
+
myRow.setValue(emlib.MDL_SHIFT_Y, deltaY)
|
|
504
|
+
myRow.addToMd(mdExp)
|
|
505
|
+
idx += 1
|
|
506
|
+
fileLabels.write(str(label - 1) + '\n')
|
|
507
|
+
|
|
508
|
+
def trainNClassifiers2ClassesStep(self, thIdx, gpuId, totalGpu):
|
|
509
|
+
|
|
510
|
+
mdNumCones = emlib.MetaData(self._getExtraPath(self._fnConeCenterDoc))
|
|
511
|
+
self.numCones = mdNumCones.size()
|
|
512
|
+
|
|
513
|
+
for i in range(self.numCones):
|
|
514
|
+
|
|
515
|
+
idx = i + 1
|
|
516
|
+
|
|
517
|
+
if (idx % totalGpu) != thIdx:
|
|
518
|
+
continue
|
|
519
|
+
|
|
520
|
+
modelFn = 'modelCone%d' % idx
|
|
521
|
+
if self.modelPretrain.get() is True:
|
|
522
|
+
if exists(self.pretrainedModels.get()._getExtraPath(modelFn + '.h5')):
|
|
523
|
+
copy(self.pretrainedModels.get()._getExtraPath(modelFn + '.h5'),
|
|
524
|
+
self._getExtraPath(modelFn + '.h5'))
|
|
525
|
+
|
|
526
|
+
expCheck = self._getProjectionsExp(idx)
|
|
527
|
+
if exists(expCheck) and not exists(self._getExtraPath(modelFn + '.h5')):
|
|
528
|
+
self._coneStep(gpuId, idx, modelFn)
|
|
529
|
+
|
|
530
|
+
def _coneStep(self, gpuId, idx, modelFn):
|
|
531
|
+
fnLabels = self._getExtraPath('labels.txt')
|
|
532
|
+
fileLabels = open(fnLabels, "r")
|
|
533
|
+
expSet = self._getProjectionsExp(self.numCones)
|
|
534
|
+
if not exists(expSet):
|
|
535
|
+
for n in range(1, self.numCones):
|
|
536
|
+
if exists(self._getProjectionsExp(self.numCones - n)):
|
|
537
|
+
expSet = self._getProjectionsExp(self.numCones - n)
|
|
538
|
+
break
|
|
539
|
+
newFnLabels = self._getExtraPath('labels%d.txt' % idx)
|
|
540
|
+
newFileLabels = open(newFnLabels, "w")
|
|
541
|
+
lines = fileLabels.readlines()
|
|
542
|
+
for line in lines:
|
|
543
|
+
if line == str(idx - 1) + '\n':
|
|
544
|
+
newFileLabels.write('1\n')
|
|
545
|
+
else:
|
|
546
|
+
newFileLabels.write('0\n')
|
|
547
|
+
newFileLabels.close()
|
|
548
|
+
fileLabels.close()
|
|
549
|
+
|
|
550
|
+
newXdim = readInfoField(self._getExtraPath(), "size",
|
|
551
|
+
emlib.MDL_XSIZE)
|
|
552
|
+
fnLabels = self._getExtraPath('labels%d.txt' % idx)
|
|
553
|
+
|
|
554
|
+
print("Training region ", idx, " in GPU ", gpuId)
|
|
555
|
+
sys.stdout.flush()
|
|
556
|
+
|
|
557
|
+
try:
|
|
558
|
+
args = "%s %s %s %s %d %d %d %d " % (
|
|
559
|
+
expSet, fnLabels, self._getExtraPath(),
|
|
560
|
+
modelFn+'_aux', self.numEpochs, newXdim, 2, self.batchSize.get())
|
|
561
|
+
#args += " %(GPU)s"
|
|
562
|
+
args += " %s " % (gpuId)
|
|
563
|
+
#args += " %s " %(int(idx % totalGpu))
|
|
564
|
+
self.runJob("xmipp_cone_deepalign", args, numberOfMpi=1, env=self.getCondaEnv())
|
|
565
|
+
except Exception as e:
|
|
566
|
+
raise Exception(
|
|
567
|
+
"ERROR: Please, if you are suffering memory problems, "
|
|
568
|
+
"check the target resolution to work with lower dimensions.")
|
|
569
|
+
|
|
570
|
+
moveFile(self._getExtraPath(modelFn + '_aux.h5'), self._getExtraPath(modelFn + '.h5'))
|
|
571
|
+
|
|
572
|
+
def predictStep(self, gpuId):
|
|
573
|
+
|
|
574
|
+
if not exists(self._getConePrediction()):
|
|
575
|
+
# if self.useQueueForSteps() or self.useQueue():
|
|
576
|
+
# myStr = os.environ["CUDA_VISIBLE_DEVICES"]
|
|
577
|
+
# else:
|
|
578
|
+
# myStr = self.gpuList.get()
|
|
579
|
+
# numGPU = myStr.split(',')
|
|
580
|
+
# numGPU = numGPU[0]
|
|
581
|
+
# print("Predict", myStr, numGPU)
|
|
582
|
+
# sys.stdout.flush()
|
|
583
|
+
|
|
584
|
+
mdNumCones = emlib.MetaData(self._getExtraPath(self._fnConeCenterDoc))
|
|
585
|
+
self.numCones = mdNumCones.size()
|
|
586
|
+
|
|
587
|
+
imgsOutStk = self._getExtraPath('images_out_filtered.stk')
|
|
588
|
+
imgsOutXmd = self._getExtraPath('images_out_filtered.xmd')
|
|
589
|
+
self.runJob("xmipp_transform_filter", " -i %s -o %s "
|
|
590
|
+
"--save_metadata_stack %s "
|
|
591
|
+
"--keep_input_columns "
|
|
592
|
+
"--fourier low_pass %f " %
|
|
593
|
+
(self.imgsFn, imgsOutStk, imgsOutXmd, 0.15), numberOfMpi=self.myMPI.get())
|
|
594
|
+
|
|
595
|
+
numMax = int(self.numConesSelected)
|
|
596
|
+
newXdim = readInfoField(self._getExtraPath(), "size",
|
|
597
|
+
emlib.MDL_XSIZE)
|
|
598
|
+
args = "%s %s %d %d %d " % (imgsOutXmd, self._getExtraPath(), newXdim, self.numCones, numMax)
|
|
599
|
+
#args += " %(GPU)s"
|
|
600
|
+
args += " %s "%(gpuId)
|
|
601
|
+
self.runJob("xmipp_cone_deepalign_predict", args, numberOfMpi=1, env=self.getCondaEnv())
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
def correlationCudaStep(self, thIdx, gpuId, totalGpu):
|
|
605
|
+
|
|
606
|
+
mdNumCones = emlib.MetaData(self._getExtraPath(self._fnConeCenterDoc))
|
|
607
|
+
self.numCones = mdNumCones.size()
|
|
608
|
+
|
|
609
|
+
# Cuda Correlation step - creating the metadata
|
|
610
|
+
predCones = np.loadtxt(self._getConePrediction())
|
|
611
|
+
mdConeList = []
|
|
612
|
+
numMax = int(self.numConesSelected)
|
|
613
|
+
for i in range(self.numCones):
|
|
614
|
+
mdConeList.append(emlib.MetaData())
|
|
615
|
+
mdIn = emlib.MetaData(self.imgsFn)
|
|
616
|
+
|
|
617
|
+
for i in range(self.numCones):
|
|
618
|
+
|
|
619
|
+
idx = i + 1
|
|
620
|
+
|
|
621
|
+
if (idx % totalGpu) != thIdx:
|
|
622
|
+
continue
|
|
623
|
+
|
|
624
|
+
#modelFn = 'modelCone%d_aux' % idx
|
|
625
|
+
#f = open(join(self._getExtraPath(), modelFn+'.txt'),'r')
|
|
626
|
+
#mae = float(f.readline())
|
|
627
|
+
#f.close()
|
|
628
|
+
|
|
629
|
+
modelFn = 'modelCone%d' % idx
|
|
630
|
+
|
|
631
|
+
positions = []
|
|
632
|
+
for n in range(numMax):
|
|
633
|
+
posAux = np.where(predCones[:, (n * 2) + 1] == (i + 1))
|
|
634
|
+
positions = positions + (np.ndarray.tolist(posAux[0]))
|
|
635
|
+
|
|
636
|
+
if len(positions) > 0 and exists(self._getExtraPath(modelFn + '.h5')):
|
|
637
|
+
print("Classifying cone ", idx, "in GPU ", gpuId)
|
|
638
|
+
|
|
639
|
+
for pos in positions:
|
|
640
|
+
id = pos + 1
|
|
641
|
+
row = md.Row()
|
|
642
|
+
row.readFromMd(mdIn, id)
|
|
643
|
+
row.addToMd(mdConeList[i])
|
|
644
|
+
fnExpCone = self._getExtraPath('metadataCone%d.xmd' % (i + 1))
|
|
645
|
+
mdConeList[i].write(fnExpCone)
|
|
646
|
+
|
|
647
|
+
fnProjCone = self._getExtraPath('projectionsCudaCorr%d.xmd' % (i + 1))
|
|
648
|
+
|
|
649
|
+
self.runJob("xmipp_metadata_utilities", "-i %s --fill ref lineal 1 1 " % (fnProjCone), numberOfMpi=1)
|
|
650
|
+
|
|
651
|
+
fnOutCone = self._getOutCone(i + 1)
|
|
652
|
+
|
|
653
|
+
if not exists(self._getExtraPath(fnOutCone)):
|
|
654
|
+
params = ' -i %s' % fnExpCone
|
|
655
|
+
params += ' -r %s' % fnProjCone
|
|
656
|
+
params += ' -o %s' % self._getExtraPath(fnOutCone)
|
|
657
|
+
params += ' --dev %s '%(gpuId)
|
|
658
|
+
self.runJob("xmipp_cuda_align_significant", params, numberOfMpi=1)
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
def correlationSignificantStep(self):
|
|
662
|
+
|
|
663
|
+
mdNumCones = emlib.MetaData(self._getExtraPath(self._fnConeCenterDoc))
|
|
664
|
+
self.numCones = mdNumCones.size()
|
|
665
|
+
|
|
666
|
+
# Cuda Correlation step - creating the metadata
|
|
667
|
+
predCones = np.loadtxt(self._getConePrediction())
|
|
668
|
+
mdConeList = []
|
|
669
|
+
numMax = int(self.numConesSelected)
|
|
670
|
+
for i in range(self.numCones):
|
|
671
|
+
mdConeList.append(emlib.MetaData())
|
|
672
|
+
mdIn = emlib.MetaData(self.imgsFn)
|
|
673
|
+
|
|
674
|
+
for i in range(self.numCones):
|
|
675
|
+
|
|
676
|
+
print("Classifying cone ", i + 1)
|
|
677
|
+
positions = []
|
|
678
|
+
for n in range(numMax):
|
|
679
|
+
posAux = np.where(predCones[:, (n * 2) + 1] == (i + 1))
|
|
680
|
+
positions = positions + (np.ndarray.tolist(posAux[0]))
|
|
681
|
+
|
|
682
|
+
if len(positions) > 0:
|
|
683
|
+
for pos in positions:
|
|
684
|
+
id = pos + 1
|
|
685
|
+
row = md.Row()
|
|
686
|
+
row.readFromMd(mdIn, id)
|
|
687
|
+
row.addToMd(mdConeList[i])
|
|
688
|
+
fnExpCone = self._getExtraPath('metadataCone%d.xmd' % (i + 1))
|
|
689
|
+
mdConeList[i].write(fnExpCone)
|
|
690
|
+
|
|
691
|
+
fnProjCone = self._getExtraPath('projectionsCudaCorr%d.xmd' % (i + 1))
|
|
692
|
+
fnOutCone = self._getOutCone(i + 1)
|
|
693
|
+
|
|
694
|
+
if not exists(self._getExtraPath(fnOutCone)):
|
|
695
|
+
# Correlation step - calling significant program
|
|
696
|
+
args = '-i %s --initgallery %s --odir %s --dontReconstruct --useForValidation %d ' \
|
|
697
|
+
'--dontCheckMirrors --maxShift 30' % (fnExpCone, fnProjCone, self._getExtraPath(), 1)
|
|
698
|
+
self.runJob('xmipp_reconstruct_significant', args,
|
|
699
|
+
numberOfMpi=self.myMPI.get())
|
|
700
|
+
copy(self._getExtraPath('images_significant_iter001_00.xmd'), self._getExtraPath(fnOutCone))
|
|
701
|
+
remove(self._getExtraPath('angles_iter001_00.xmd'))
|
|
702
|
+
remove(self._getExtraPath('images_significant_iter001_00.xmd'))
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
def createOutputMetadataStep(self):
|
|
706
|
+
|
|
707
|
+
mdNumCones = emlib.MetaData(self._getExtraPath(self._fnConeCenterDoc))
|
|
708
|
+
self.numCones = mdNumCones.size()
|
|
709
|
+
numMax = int(self.numConesSelected)
|
|
710
|
+
mdIn = emlib.MetaData(self.imgsFn)
|
|
711
|
+
allInFns = mdIn.getColumnValues(emlib.MDL_IMAGE)
|
|
712
|
+
|
|
713
|
+
coneFns = []
|
|
714
|
+
coneCCs = []
|
|
715
|
+
mdCones = []
|
|
716
|
+
shiftX = []
|
|
717
|
+
shiftY = []
|
|
718
|
+
fnFinal = self._getExtraPath('outConesParticles.xmd')
|
|
719
|
+
for i in range(self.numCones):
|
|
720
|
+
|
|
721
|
+
fnOutCone = self._getOutCone(i + 1)
|
|
722
|
+
|
|
723
|
+
if exists(self._getExtraPath(fnOutCone)):
|
|
724
|
+
self._updateCone(numMax, coneFns, coneCCs, mdCones, shiftX, shiftY, fnFinal, i, fnOutCone)
|
|
725
|
+
elif numMax > 1:
|
|
726
|
+
mdCones.append(None)
|
|
727
|
+
coneFns.append([])
|
|
728
|
+
coneCCs.append([])
|
|
729
|
+
shiftX.append([])
|
|
730
|
+
shiftY.append([])
|
|
731
|
+
|
|
732
|
+
if numMax > 1:
|
|
733
|
+
self._writeMDFinal(allInFns, coneFns, coneCCs, mdCones, shiftX, shiftY, fnFinal)
|
|
734
|
+
else:
|
|
735
|
+
mdCones = emlib.MetaData(fnFinal)
|
|
736
|
+
mdCones.removeObjects(emlib.MDValueGT(emlib.MDL_SHIFT_X, 30.))
|
|
737
|
+
mdCones.removeObjects(emlib.MDValueGT(emlib.MDL_SHIFT_Y, 30.))
|
|
738
|
+
mdCones.removeObjects(emlib.MDValueLT(emlib.MDL_SHIFT_X, -30.))
|
|
739
|
+
mdCones.removeObjects(emlib.MDValueLT(emlib.MDL_SHIFT_Y, -30.))
|
|
740
|
+
mdCones.write(fnFinal)
|
|
741
|
+
|
|
742
|
+
def _updateCone(self, numMax, coneFns, coneCCs, mdCones, shiftX, shiftY, fnFinal, i, fnOutCone):
|
|
743
|
+
if numMax == 1:
|
|
744
|
+
if not exists(fnFinal):
|
|
745
|
+
copy(self._getExtraPath(fnOutCone), fnFinal)
|
|
746
|
+
else:
|
|
747
|
+
params = ' -i %s --set union %s -o %s' % (fnFinal, self._getExtraPath(fnOutCone),
|
|
748
|
+
fnFinal)
|
|
749
|
+
self.runJob("xmipp_metadata_utilities", params,
|
|
750
|
+
numberOfMpi=1)
|
|
751
|
+
else:
|
|
752
|
+
mdCones.append(emlib.MetaData(self._getExtraPath(fnOutCone)))
|
|
753
|
+
coneFns.append(mdCones[i].getColumnValues(emlib.MDL_IMAGE))
|
|
754
|
+
shiftX.append(mdCones[i].getColumnValues(emlib.MDL_SHIFT_X))
|
|
755
|
+
shiftY.append(mdCones[i].getColumnValues(emlib.MDL_SHIFT_Y))
|
|
756
|
+
coneCCs.append(mdCones[i].getColumnValues(emlib.MDL_MAXCC))
|
|
757
|
+
|
|
758
|
+
def _writeMDFinal(self, allInFns, coneFns, coneCCs, mdCones, shiftX, shiftY, fnFinal):
|
|
759
|
+
mdFinal = emlib.MetaData()
|
|
760
|
+
row = md.Row()
|
|
761
|
+
for myFn in allInFns:
|
|
762
|
+
myCCs = []
|
|
763
|
+
myCones = []
|
|
764
|
+
myPos = []
|
|
765
|
+
for n in range(self.numCones):
|
|
766
|
+
if myFn in coneFns[n]:
|
|
767
|
+
pos = coneFns[n].index(myFn)
|
|
768
|
+
myPos.append(pos)
|
|
769
|
+
if abs(shiftX[n][pos])<30 and abs(shiftY[n][pos])<30:
|
|
770
|
+
myCCs.append(coneCCs[n][pos])
|
|
771
|
+
else:
|
|
772
|
+
myCCs.append(0)
|
|
773
|
+
myCones.append(n + 1)
|
|
774
|
+
if len(myPos) > 0:
|
|
775
|
+
if max(myCCs)==0:
|
|
776
|
+
continue
|
|
777
|
+
coneMax = myCones[myCCs.index(max(myCCs))]
|
|
778
|
+
objId = myPos[myCCs.index(max(myCCs))] + 1
|
|
779
|
+
row.readFromMd(mdCones[coneMax - 1], objId)
|
|
780
|
+
row.addToMd(mdFinal)
|
|
781
|
+
mdFinal.write(fnFinal)
|
|
782
|
+
|
|
783
|
+
def createOutputStep(self):
|
|
784
|
+
|
|
785
|
+
cleanPattern(self._getExtraPath('*.stk'))
|
|
786
|
+
cleanPattern(self._getExtraPath('projectionsCudaCorr*'))
|
|
787
|
+
|
|
788
|
+
inputParticles = self.inputSet.get()
|
|
789
|
+
fnOutputParticles = self._getExtraPath('outConesParticles.xmd')
|
|
790
|
+
|
|
791
|
+
outputSetOfParticles = self._createSetOfParticles()
|
|
792
|
+
outputSetOfParticles.copyInfo(inputParticles)
|
|
793
|
+
outputSetOfParticles.setAlignmentProj()
|
|
794
|
+
|
|
795
|
+
Xdim = inputParticles.getXDim()
|
|
796
|
+
newXdim = readInfoField(self._getExtraPath(), "size",
|
|
797
|
+
emlib.MDL_XSIZE)
|
|
798
|
+
Ts = readInfoField(self._getExtraPath(), "sampling",
|
|
799
|
+
emlib.MDL_SAMPLINGRATE)
|
|
800
|
+
if newXdim != Xdim:
|
|
801
|
+
self.scaleFactor = Ts / inputParticles.getSamplingRate()
|
|
802
|
+
self.iterMd = md.iterRows(fnOutputParticles, emlib.MDL_ITEM_ID)
|
|
803
|
+
self.lastRow = next(self.iterMd)
|
|
804
|
+
outputSetOfParticles.copyItems(inputParticles,
|
|
805
|
+
updateItemCallback=self._updateItem)
|
|
806
|
+
else:
|
|
807
|
+
readSetOfParticles(fnOutputParticles, outputSetOfParticles)
|
|
808
|
+
self._defineOutputs(outputParticles=outputSetOfParticles)
|
|
809
|
+
|
|
810
|
+
def _updateItem(self, particle, row):
|
|
811
|
+
count = 0
|
|
812
|
+
while self.lastRow and particle.getObjId() == self.lastRow.getValue(
|
|
813
|
+
emlib.MDL_ITEM_ID):
|
|
814
|
+
count += 1
|
|
815
|
+
if count:
|
|
816
|
+
self._createItemMatrix(particle, self.lastRow)
|
|
817
|
+
try:
|
|
818
|
+
self.lastRow = next(self.iterMd)
|
|
819
|
+
except StopIteration:
|
|
820
|
+
self.lastRow = None
|
|
821
|
+
particle._appendItem = count > 0
|
|
822
|
+
|
|
823
|
+
def _createItemMatrix(self, particle, row):
|
|
824
|
+
|
|
825
|
+
row.setValue(emlib.MDL_SHIFT_X,
|
|
826
|
+
row.getValue(emlib.MDL_SHIFT_X) * self.scaleFactor)
|
|
827
|
+
row.setValue(emlib.MDL_SHIFT_Y,
|
|
828
|
+
row.getValue(emlib.MDL_SHIFT_Y) * self.scaleFactor)
|
|
829
|
+
setXmippAttributes(particle, row, emlib.MDL_SHIFT_X,
|
|
830
|
+
emlib.MDL_SHIFT_Y,
|
|
831
|
+
emlib.MDL_ANGLE_ROT, emlib.MDL_ANGLE_TILT,
|
|
832
|
+
emlib.MDL_ANGLE_PSI)
|
|
833
|
+
createItemMatrix(particle, row, align=ALIGN_PROJ)
|
|
834
|
+
|
|
835
|
+
# --------------------------- INFO functions --------------------------------
|
|
836
|
+
def _summary(self):
|
|
837
|
+
summary = []
|
|
838
|
+
summary.append("Images evaluated: %i" % self.inputSet.get().getSize())
|
|
839
|
+
summary.append("Volume: %s" % self.inputVolume.getNameId())
|
|
840
|
+
return summary
|
|
841
|
+
|
|
842
|
+
def _methods(self):
|
|
843
|
+
methods = []
|
|
844
|
+
if hasattr(self, 'outputParticles'):
|
|
845
|
+
methods.append("We classify %i input images %s regarding to volume %s." \
|
|
846
|
+
% (self.inputSet.get().getSize(), self.getObjectTag('inputSet'),
|
|
847
|
+
self.getObjectTag('inputVolume')))
|
|
848
|
+
return methods
|
|
849
|
+
|
|
850
|
+
def _validate(self):
|
|
851
|
+
errors = []
|
|
852
|
+
if self.numberOfMpi>1:
|
|
853
|
+
errors.append("You must select Threads to make the parallelization in Scipion level. "
|
|
854
|
+
"To parallelize the Xmipp program use MPIs in the form.")
|
|
855
|
+
if self.spanConesTilt.get()>30:
|
|
856
|
+
errors.append("The distance between region centers should be lower than 31 degress.")
|
|
857
|
+
return errors
|
|
858
|
+
|
|
859
|
+
|