Skip to content

Commit

Permalink
Merge branch 'devel' into fix_test_score_coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
albertmena committed Nov 2, 2023
2 parents 5f819c9 + 4cec6e7 commit 770c0fb
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 50 deletions.
35 changes: 20 additions & 15 deletions xmipptomo/protocols/protocol_cltomo.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,46 @@
from pyworkflow.protocol import (IntParam, PointerParam, LEVEL_ADVANCED, BooleanParam, StringParam, FloatParam)
from pyworkflow.utils import Environ
from xmipp3 import Plugin
from pwem.protocols import EMProtocol
from tomo.protocols import ProtTomoBase
from tomo.objects import SetOfSubTomograms, SetOfClassesSubTomograms
from xmipp3.convert import readSetOfVolumes, readSetOfClassesVol, writeSetOfVolumes

OUTPUTATTRIBUTE ='SetOfClassesSubTomograms'

class XmippProtCLTomo(ProtClassify3D):
class XmippProtCLTomo(EMProtocol, ProtTomoBase):
""" Averages a set of subtomograms taking into account the missing edge. """

_label = 'cltomo'
_devStatus = BETA
_possibleOutputs = {OUTPUTATTRIBUTE: SetOfClassesSubTomograms}

# --------------------------- DEFINE param functions --------------------------------------------
def _defineParams(self, form):
form.addSection(label='General parameters')
form.addParam('inputVolumes', PointerParam, pointerClass="SetOfVolumes", label='Set of volumes',
help="Set of volumes to align")
form.addParam('inputVolumes', PointerParam, pointerClass=SetOfSubTomograms, label='Subtomograms',
help="Set of subtomograms to align")
form.addParam('numberOfReferences', IntParam, label='Number of references', default=3,
help="How many references are computed at the end of the process")
form.addParam('numberOfIterations', IntParam, label='Number of iterations', default=15,
expertLevel=LEVEL_ADVANCED, help="How many iterations at each of the Clustering levels")
form.addParam('generateAligned', BooleanParam, default=True, label='Generate aligned subvolumes',
form.addParam('generateAligned', BooleanParam, default=True, label='Generate aligned subtomograms',
help="If set to true, it will be created a new set of volumes with all of them aligned")
form.addParam('align', BooleanParam, default=True, label="Align",
help="Do not align if volumes are already aligned, only classify")
help="Do not align if subtomograms are already aligned, only classify")

form.addSection(label='Initial references')
form.addParam('doGenerateInitial', BooleanParam, default=True, label='Generate initial volume',
help="Let CLTomo to automatically generate the initial references")
form.addParam('numberOfReferences0', IntParam, label='Number of initial references', default=1,
condition="doGenerateInitial",
help="How many initial volumes. If set to 1, all subvolumes are aligned to a single reference, " \
help="How many initial subtomograms. If set to 1, all subtomograms are aligned to a single reference, " \
"and then they are classified")
form.addParam('randomizeOrientation', BooleanParam, default=False, label='Randomize orientation',
condition="doGenerateInitial",
help="Use this option if all the input volumes have the same missing wedge or if they have not been previously aligned.")
form.addParam('referenceList', PointerParam, pointerClass="SetOfVolumes", label='Set of initial volumes',
condition="not doGenerateInitial", help="Set of initial volumes")
help="Use this option if all the input subtomograms have the same missing wedge or if they have not been previously aligned.")
form.addParam('referenceList', PointerParam, pointerClass="SetOfVolumes", label='Set of initial subtomograms',
condition="not doGenerateInitial", help="Set of initial subtomograms")

form.addSection(label='Constraints')
form.addParam('symmetry', StringParam, default='c1', label='Symmetry group',
Expand Down Expand Up @@ -134,7 +139,7 @@ def createOutput(self):
if levelFiles:
levelFiles.sort()
lastLevelFile = levelFiles[-1]
setOfClasses = self._createSetOfClassesVol()
setOfClasses = self._createSetOfClassesSubTomograms(self.inputVolumes.get())
setOfClasses.setImages(self.inputVolumes.get())
readSetOfClassesVol(setOfClasses, lastLevelFile)
self._defineOutputs(outputClasses=setOfClasses)
Expand All @@ -154,27 +159,27 @@ def _summary(self):
if self.doGenerateInitial.get():
messages.append('Number of initial references: %d' % self.numberOfReferences0.get())
if self.randomizeOrientation.get():
messages.append('Input subvolume orientations were randomized')
messages.append('Input subtomograms orientations were randomized')
if not self.align:
messages.append('Input subvolumes were assumed to be already aligned')
messages.append('Input subtomograms were assumed to be already aligned')
messages.append('Number of output references: %d' % self.numberOfReferences.get())
return messages

def _validate(self):
errors = []
(Xdim1, Ydim1, Zdim1) = self.inputVolumes.get().getDimensions()
if Xdim1 != Ydim1 or Ydim1 != Zdim1:
errors.append("Input subvolumes are not cubic")
errors.append("Input subtomograms are not cubic")
N0 = -1
if not self.doGenerateInitial.get():
if not self.referenceList.hasValue():
errors.append("If references are not self generated, you have to provide a reference set of volumes")
else:
(Xdim2, Ydim2, Zdim2) = self.referenceList.get().getDimensions()
if Xdim2 != Ydim2 or Ydim2 != Zdim2:
errors.append("Reference subvolumes are not cubic")
errors.append("Reference subtomograms are not cubic")
if Xdim1 != Xdim2:
errors.append("Input and reference subvolumes are of different size")
errors.append("Input and reference subtomograms are of different size")
else:
N0 = self.numberOfReferences0.get()
if N0 > 0 and N0 > self.numberOfReferences.get():
Expand Down
File renamed without changes.
113 changes: 113 additions & 0 deletions xmipptomo/tests/test_protocol_cltomo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# **************************************************************************
# *
# * Authors: J.L. Vilas ([email protected])
# *
# * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 3 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# * 02111-1307 USA
# *
# * All comments concerning this program package may be sent to the
# * e-mail address '[email protected]'
# *
# **************************************************************************
from pyworkflow.tests import BaseTest, setupTestProject
from tomo.protocols.protocol_import_coordinates import IMPORT_FROM_EMAN
import tomo.protocols

from xmipptomo.protocols.protocol_cltomo import XmippProtCLTomo
from xmipptomo.protocols.protocol_extract_subtomos import XmippProtExtractSubtomos

## Tomogram type constants for particle extraction
#OUTPUTATTRIBUTE = 'Subtomograms'

class TestXmippProtCLTomoBase(BaseTest):
@classmethod
def setUpClass(cls):
setupTestProject(cls)

@classmethod
def setData(cls, projectData='tomo-em'):
from tomo.tests import DataSet
cls.dataset = DataSet.getDataSet(projectData)
cls.tomogram = cls.dataset.getFile('tomo1')
cls.coords3D = cls.dataset.getFile('overview_wbp.txt')
cls.coords3D_Large = cls.dataset.getFile('overview_wbp_large.txt')
cls.inputSetOfSubTomogram = cls.dataset.getFile('subtomo')
cls.smallTomogram = cls.dataset.getFile('coremask_normcorona.mrc')


class TestXmippProtCLtomo(TestXmippProtCLTomoBase):
"""
This prepares the protocols to perform the necessary tests.
"""

@classmethod
def setUpClass(cls):
setupTestProject(cls)
TestXmippProtCLtomo.setData()

def _runImportCoordinatesAndTomograms(self):
protImportTomogram = self.newProtocol(tomo.protocols.ProtImportTomograms,
filesPath=self.tomogram,
samplingRate=5)

self.launchProtocol(protImportTomogram)

protImportCoordinates3d = self.newProtocol(tomo.protocols.ProtImportCoordinates3D,
auto=IMPORT_FROM_EMAN,
filesPath=self.coords3D,
importTomograms=protImportTomogram.Tomograms,
filesPattern='', boxSize=32,
samplingRate=5)

self.launchProtocol(protImportCoordinates3d)
self.assertSetSize(protImportTomogram.Tomograms, 1,
"There was a problem with tomogram output")
self.assertSetSize(protImportCoordinates3d.outputCoordinates, 5,
"There was a problem with coordinates 3d output")

protTomoExtraction = self.newProtocol(XmippProtExtractSubtomos,
tomograms=protImportTomogram.Tomograms,
coords=protImportCoordinates3d.outputCoordinates,
invertContrast=True,
boxSize=32)

self.launchProtocol(protTomoExtraction)
self.assertSetSize(getattr(protTomoExtraction, 'Subtomograms'), 5,
"There was a problem with SetOfSubtomogram output")

return protTomoExtraction

def _runXmippCLtomo(self):
protTomoExtraction = self._runImportCoordinatesAndTomograms()
protCLTomo = self.newProtocol(XmippProtCLTomo, inputVolumes=protTomoExtraction.Subtomograms)

self.launchProtocol(protCLTomo)


return protCLTomo


class TestXmippProtCLtomo(TestXmippProtCLtomo):
"""
This class check if CLtomos in Xmipptomo works properly.
"""

def test_CLtomo(self):
protCLTomo = self._runXmippCLtomo()
msg = "There was a problem with CLtomo"
self.assertIsNotNone(protCLTomo.outputClasses, msg)

Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
# * e-mail address '[email protected]'
# *
# **************************************************************************
from os.path import exists, join, split

from pyworkflow.tests import BaseTest, DataSet, setupTestProject
from tomo.protocols import ProtImportTomograms
from xmipptomo.protocols import XmippProtCropTomograms
from xmipptomo.protocols.protocol_crop_tomograms import SUFIXCROPPED


class TestReSizeBase(BaseTest):
Expand Down Expand Up @@ -55,16 +54,15 @@ def setUpClass(cls):
cls.protImportTomos = cls.runImportTomograms(cls.tomos, 16.14)

def testCropTomogramsSamplingRate(self):
Rrb = XmippProtCropTomograms()
crop = self.newProtocol(XmippProtCropTomograms,
objLabel='crop tomos',
inputSet=self.protImportTomos.Tomograms,
xcrop0=100,
xcropF=900,
ycrop0=100,
ycropF=900,
zcrop0=10,
zcropF=100)
objLabel='crop tomos',
inputSet=self.protImportTomos.Tomograms,
xcrop0=100,
xcropF=900,
ycrop0=100,
ycropF=900,
zcrop0=10,
zcropF=100)
self.launchProtocol(crop)

self.assertTrue(crop)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
# *
# *****************************************************************************

from pyworkflow.tests import *
import os

from pyworkflow.tests import setupTestProject, DataSet, BaseTest
from tomo.protocols.protocol_import_tomograms import ProtImportTomograms
from xmipptomo.protocols.protocol_peak_high_contrast import XmippProtPeakHighContrast
from xmipptomo.protocols.protocol_deep_misalignment_detection import XmippProtDeepDetectMisalignment
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
# * e-mail address '[email protected]'
# *
# **************************************************************************
from os.path import exists, join, split

from pyworkflow.tests import BaseTest, DataSet, setupTestProject
from tomo.protocols import ProtImportTomograms
from xmipptomo.protocols import XmippProtResizeTomograms, XmippProtResizeTiltSeries
from xmipptomo.protocols.protocol_crop_resize_base import XmippProtResizeBase
import os
from xmipptomo.protocols import XmippProtResizeTomograms


class TestReSizeBase(BaseTest):
Expand All @@ -49,51 +47,49 @@ def runImportTomograms(cls, pattern, samplingRate):


class TestReSizeTomograms(TestReSizeBase):
_objLabel = 'Resize tomos'

@classmethod
def setUpClass(cls):
setupTestProject(cls)
TestReSizeBase.setData()
cls.protImportTomos = cls.runImportTomograms(cls.tomos, 16.14)

def testReSizeTomogramsSamplingRate(self):
Rrb = XmippProtResizeTomograms()
protResizeTomos = XmippProtResizeTomograms()
reSize = self.newProtocol(XmippProtResizeTomograms,
objLabel='Resize tomos',
inputSet=self.protImportTomos.Tomograms,
resizeOption = Rrb.RESIZE_SAMPLINGRATE,
resizeSamplingRate = 32.28)
objLabel=self._objLabel,
inputSet=self.protImportTomos.Tomograms,
resizeOption=protResizeTomos.RESIZE_SAMPLINGRATE,
resizeSamplingRate=32.28)
self.launchProtocol(reSize)
self.assertTrue(reSize)
self.assertSetSize(reSize.outputSetOfTomograms, 2,
"resize has failed in the samplingrate option probably related with the use "
"of a SetOfTomograms (processing the second tomogram)")


def testReSizeTomogramsFactor(self):
Rrb = XmippProtResizeTomograms()
protResizeTomos = XmippProtResizeTomograms()
reSize = self.newProtocol(XmippProtResizeTomograms,
objLabel='Resize tomos',
inputSet=self.protImportTomos.Tomograms,
resizeOption = Rrb.RESIZE_FACTOR,
resizeFactor = 0.5)
objLabel=self._objLabel,
inputSet=self.protImportTomos.Tomograms,
resizeOption=protResizeTomos.RESIZE_FACTOR,
resizeFactor=0.5)
self.launchProtocol(reSize)
self.assertTrue(reSize)
self.assertSetSize(reSize.outputSetOfTomograms, 2,
"resize has failed in the Factor option probably related with the use "
"of a SetOfTomograms (processing the second tomogram)")


def testReSizeTomogramsPiramid(self):
Rrb = XmippProtResizeTomograms()
protResizeTomos = XmippProtResizeTomograms()
reSize = self.newProtocol(XmippProtResizeTomograms,
objLabel='Resize tomos',
inputSet=self.protImportTomos.Tomograms,
resizeOption = Rrb.RESIZE_PYRAMID,
resizeLevel = 0)
objLabel=self._objLabel,
inputSet=self.protImportTomos.Tomograms,
resizeOption=protResizeTomos.RESIZE_PYRAMID,
resizeLevel=0)
self.launchProtocol(reSize)
self.assertTrue(reSize)
self.assertSetSize(reSize.outputSetOfTomograms, 2,
"Resize has failed in the pyramid option probably related with the use "
"of a SetOfTomograms (processing the second tomogram)")


File renamed without changes.

0 comments on commit 770c0fb

Please sign in to comment.