forked from I2PC/scipion
/
convert.py
1358 lines (1086 loc) · 48.1 KB
/
convert.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# **************************************************************************
# *
# * Authors: J.M. De la Rosa Trevin (jmdelarosa@cnb.csic.es)
# * Laura del Cano (ldelcano@cnb.csic.es)
# *
# * 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 2 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 'jmdelarosa@cnb.csic.es'
# *
# **************************************************************************
"""
This module contains converter functions that will serve to:
1. Write from base classes to Xmipp specific files
2. Read from Xmipp files to base classes
"""
import os
from os.path import join, dirname
from collections import OrderedDict
from itertools import izip
import numpy
import xmipp
from pyworkflow.em.packages.xmipp3.utils import iterMdRows
from xmipp3 import XmippMdRow, getLabelPythonType, RowMetaData
from pyworkflow.em import *
from pyworkflow.utils.path import replaceBaseExt, removeExt, findRootFrom
# This dictionary will be used to map
# between CTFModel properties and Xmipp labels
ACQUISITION_DICT = OrderedDict([
("_amplitudeContrast", xmipp.MDL_CTF_Q0),
("_sphericalAberration", xmipp.MDL_CTF_CS),
("_voltage", xmipp.MDL_CTF_VOLTAGE)
])
COOR_DICT = OrderedDict([
("_x", xmipp.MDL_XCOOR),
("_y", xmipp.MDL_YCOOR)
])
CTF_DICT = OrderedDict([
("_defocusU", xmipp.MDL_CTF_DEFOCUSU),
("_defocusV", xmipp.MDL_CTF_DEFOCUSV),
("_defocusAngle", xmipp.MDL_CTF_DEFOCUS_ANGLE)
])
CTF_PSD_DICT = OrderedDict([
("_psdFile", xmipp.MDL_PSD),
("_xmipp_enhanced_psd", xmipp.MDL_PSD_ENHANCED),
("_xmipp_ctfmodel_quadrant", xmipp.MDL_IMAGE1),
("_xmipp_ctfmodel_halfplane", xmipp.MDL_IMAGE1)
])
CTF_EXTRA_LABELS = [
xmipp.MDL_CTF_CA,
xmipp.MDL_CTF_ENERGY_LOSS,
xmipp.MDL_CTF_LENS_STABILITY,
xmipp.MDL_CTF_CONVERGENCE_CONE,
xmipp.MDL_CTF_LONGITUDINAL_DISPLACEMENT,
xmipp.MDL_CTF_TRANSVERSAL_DISPLACEMENT,
xmipp.MDL_CTF_K,
xmipp.MDL_CTF_BG_GAUSSIAN_K,
xmipp.MDL_CTF_BG_GAUSSIAN_SIGMAU,
xmipp.MDL_CTF_BG_GAUSSIAN_SIGMAV,
xmipp.MDL_CTF_BG_GAUSSIAN_CU,
xmipp.MDL_CTF_BG_GAUSSIAN_CV,
xmipp.MDL_CTF_BG_SQRT_K,
xmipp.MDL_CTF_BG_SQRT_U,
xmipp.MDL_CTF_BG_SQRT_V,
xmipp.MDL_CTF_BG_SQRT_ANGLE,
xmipp.MDL_CTF_BG_BASELINE,
xmipp.MDL_CTF_BG_GAUSSIAN2_K,
xmipp.MDL_CTF_BG_GAUSSIAN2_SIGMAU,
xmipp.MDL_CTF_BG_GAUSSIAN2_SIGMAV,
xmipp.MDL_CTF_BG_GAUSSIAN2_CU,
xmipp.MDL_CTF_BG_GAUSSIAN2_CV,
xmipp.MDL_CTF_BG_GAUSSIAN2_ANGLE,
xmipp.MDL_CTF_CRIT_FITTINGSCORE,
xmipp.MDL_CTF_CRIT_FITTINGCORR13,
xmipp.MDL_CTF_DOWNSAMPLE_PERFORMED,
xmipp.MDL_CTF_CRIT_PSDVARIANCE,
xmipp.MDL_CTF_CRIT_PSDPCA1VARIANCE,
xmipp.MDL_CTF_CRIT_PSDPCARUNSTEST,
xmipp.MDL_CTF_CRIT_FIRSTZEROAVG,
xmipp.MDL_CTF_CRIT_DAMPING,
xmipp.MDL_CTF_CRIT_FIRSTZERORATIO,
xmipp.MDL_CTF_CRIT_PSDCORRELATION90,
xmipp.MDL_CTF_CRIT_PSDRADIALINTEGRAL,
xmipp.MDL_CTF_CRIT_NORMALITY,
# In xmipp the ctf also contains acquisition information
xmipp.MDL_CTF_Q0,
xmipp.MDL_CTF_CS,
xmipp.MDL_CTF_VOLTAGE,
xmipp.MDL_CTF_SAMPLING_RATE
]
# Some extra labels to take into account the zscore
IMAGE_EXTRA_LABELS = [
xmipp.MDL_ZSCORE,
xmipp.MDL_ZSCORE_HISTOGRAM,
xmipp.MDL_ZSCORE_RESMEAN,
xmipp.MDL_ZSCORE_RESVAR,
xmipp.MDL_ZSCORE_RESCOV,
xmipp.MDL_ZSCORE_SHAPE1,
xmipp.MDL_ZSCORE_SHAPE2,
xmipp.MDL_ZSCORE_SNR1,
xmipp.MDL_ZSCORE_SNR2,
xmipp.MDL_CUMULATIVE_SSNR,
xmipp.MDL_PARTICLE_ID,
xmipp.MDL_FRAME_ID,
]
ANGLES_DICT = OrderedDict([
("_angleY", xmipp.MDL_ANGLE_Y),
("_angleY2", xmipp.MDL_ANGLE_Y2),
("_angleTilt", xmipp.MDL_ANGLE_TILT)
])
ALIGNMENT_DICT = OrderedDict([
("_xmipp_shiftX", xmipp.MDL_SHIFT_X),
("_xmipp_shiftY", xmipp.MDL_SHIFT_Y),
("_xmipp_shiftZ", xmipp.MDL_SHIFT_Z),
("_xmipp_flip", xmipp.MDL_FLIP),
("_xmipp_anglePsi", xmipp.MDL_ANGLE_PSI),
("_xmipp_angleRot", xmipp.MDL_ANGLE_ROT),
("_xmipp_angleTilt", xmipp.MDL_ANGLE_TILT),
])
def objectToRow(obj, row, attrDict, extraLabels={}):
""" This function will convert an EMObject into a XmippMdRow.
Params:
obj: the EMObject instance (input)
row: the XmippMdRow instance (output)
attrDict: dictionary with the map between obj attributes(keys) and
row MDLabels in Xmipp (values).
extraLabels: a list with extra labels that could be included
as _xmipp_labelName
"""
if obj.isEnabled():
enabled = 1
else:
enabled = -1
row.setValue(xmipp.MDL_ENABLED, enabled)
for attr, label in attrDict.iteritems():
if hasattr(obj, attr):
valueType = getLabelPythonType(label)
row.setValue(label, valueType(getattr(obj, attr).get()))
attrLabels = attrDict.values()
for label in extraLabels:
attrName = '_xmipp_%s' % xmipp.label2Str(label)
if label not in attrLabels and hasattr(obj, attrName):
value = obj.getAttributeValue(attrName)
row.setValue(label, value)
def rowToObject(row, obj, attrDict, extraLabels={}):
""" This function will convert from a XmippMdRow to an EMObject.
Params:
row: the XmippMdRow instance (input)
obj: the EMObject instance (output)
attrDict: dictionary with the map between obj attributes(keys) and
row MDLabels in Xmipp (values).
extraLabels: a list with extra labels that could be included
as _xmipp_labelName
"""
obj.setEnabled(row.getValue(xmipp.MDL_ENABLED, 1) > 0)
for attr, label in attrDict.iteritems():
value = row.getValue(label)
if not hasattr(obj, attr):
setattr(obj, attr, ObjectWrap(value))
else:
getattr(obj, attr).set(value)
attrLabels = attrDict.values()
for label in extraLabels:
if label not in attrLabels and row.hasLabel(label):
labelStr = xmipp.label2Str(label)
setattr(obj, '_xmipp_%s' % labelStr, row.getValueAsObject(label))
def rowFromMd(md, objId):
row = XmippMdRow()
row.readFromMd(md, objId)
return row
def _containsAll(row, labels):
""" Check if the labels (values) in labelsDict
are present in the row.
"""
values = labels.values() if isinstance(labels, dict) else labels
return all(row.containsLabel(l) for l in values)
def _containsAny(row, labels):
""" Check if the labels (values) in labelsDict
are present in the row.
"""
values = labels.values() if isinstance(labels, dict) else labels
return any(row.containsLabel(l) for l in values)
def _rowToObjectFunc(obj):
""" From a given object, return the function rowTo{OBJECT_CLASS_NAME}. """
return globals()['rowTo' + obj.getClassName()]
def _readSetFunc(obj):
""" From a given object, return the function read{OBJECT_CLASS_NAME}. """
return globals()['read' + obj.getClassName()]
def locationToXmipp(index, filename):
""" Convert an index and filename location
to a string with @ as expected in Xmipp.
"""
#TODO: Maybe we need to add more logic dependent of the format
if index != NO_INDEX:
return "%06d@%s" % (index, filename)
return filename
def fixVolumeFileName(image):
""" This method will add :mrc to .mrc volumes
because for mrc format is not possible to distinguish
between 3D volumes and 2D stacks.
"""
fn = image.getFileName()
if isinstance(image, Volume):
if fn.endswith('.mrc') or fn.endswith('.map'):
fn += ':mrc'
return fn
def getImageLocation(image):
xmippFn = locationToXmipp(image.getIndex(),
fixVolumeFileName(image))
return xmippFn
def xmippToLocation(xmippFilename):
""" Return a location (index, filename) given
a Xmipp filename with the index@filename structure. """
if '@' in xmippFilename:
return xmipp.FileName(xmippFilename).decompose()
else:
return NO_INDEX, str(xmippFilename)
def setObjId(obj, mdRow, label=xmipp.MDL_ITEM_ID):
if mdRow.containsLabel(label):
obj.setObjId(mdRow.getValue(label))
else:
obj.setObjId(None)
def setRowId(mdRow, obj, label=xmipp.MDL_ITEM_ID):
mdRow.setValue(label, long(obj.getObjId()))
def micrographToCTFParam(mic, ctfparam):
""" This function is used to convert a Micrograph object
to the .ctfparam metadata needed by some Xmipp programs.
If the micrograph already comes from xmipp, the ctfparam
will be returned, if not, the new file.
"""
ctf = mic.getCTF()
if hasattr(ctf, '_xmippMd'):
return ctf._xmippMd.get()
md = xmipp.MetaData()
md.setColumnFormat(False)
row = XmippMdRow()
ctfModelToRow(ctf, row)
acquisitionToRow(mic.getAcquisition(), row)
row.writeToMd(md, md.addObject())
md.write(ctfparam)
return ctfparam
def imageToRow(img, imgRow, imgLabel, **kwargs):
# Provide a hook to be used if something is needed to be
# done for special cases before converting image to row
preprocessImageRow = kwargs.get('preprocessImageRow', None)
if preprocessImageRow:
preprocessImageRow(img, imgRow)
setRowId(imgRow, img) # Set the id in the metadata as MDL_ITEM_ID
index, filename = img.getLocation()
fn = locationToXmipp(index, filename)
imgRow.setValue(imgLabel, fn)
if kwargs.get('writeCtf', True) and img.hasCTF():
ctfModelToRow(img.getCTF(), imgRow)
# alignment is mandatory at this point, it shoud be check
# and detected defaults if not passed at readSetOf.. level
alignType = kwargs.get('alignType')
if alignType != ALIGN_NONE:
alignmentToRow(img.getTransform(), imgRow, alignType)
if kwargs.get('writeAcquisition', True) and img.hasAcquisition():
acquisitionToRow(img.getAcquisition(), imgRow)
# Write all extra labels to the row
objectToRow(img, imgRow, {}, extraLabels=IMAGE_EXTRA_LABELS)
# Provide a hook to be used if something is needed to be
# done for special cases before converting image to row
postprocessImageRow = kwargs.get('postprocessImageRow', None)
if postprocessImageRow:
postprocessImageRow(img, imgRow)
def rowToImage(imgRow, imgLabel, imgClass, **kwargs):
""" Create an Image from a row of a metadata. """
img = imgClass()
# Provide a hook to be used if something is needed to be
# done for special cases before converting image to row
preprocessImageRow = kwargs.get('preprocessImageRow', None)
if preprocessImageRow:
preprocessImageRow(img, imgRow)
# Decompose Xmipp filename
index, filename = xmippToLocation(imgRow.getValue(imgLabel))
img.setLocation(index, filename)
if imgRow.containsLabel(xmipp.MDL_REF):
img.setClassId(imgRow.getValue(xmipp.MDL_REF))
elif imgRow.containsLabel(xmipp.MDL_REF3D):
img.setClassId(imgRow.getValue(xmipp.MDL_REF3D))
if kwargs.get('readCtf', True):
img.setCTF(rowToCtfModel(imgRow))
# alignment is mandatory at this point, it shoud be check
# and detected defaults if not passed at readSetOf.. level
alignType = kwargs.get('alignType')
if alignType != ALIGN_NONE:
img.setTransform(rowToAlignment(imgRow, alignType))
if kwargs.get('readAcquisition', True):
img.setAcquisition(rowToAcquisition(imgRow))
if kwargs.get('magnification', None):
img.getAcquisition().setMagnification(kwargs.get("magnification"))
setObjId(img, imgRow)
# Read some extra labels
rowToObject(imgRow, img, {},
extraLabels=IMAGE_EXTRA_LABELS + kwargs.get('extraLabels', []))
# Provide a hook to be used if something is needed to be
# done for special cases before converting image to row
postprocessImageRow = kwargs.get('postprocessImageRow', None)
if postprocessImageRow:
postprocessImageRow(img, imgRow)
return img
def micrographToRow(mic, micRow, **kwargs):
""" Set labels values from Micrograph mic to md row. """
imageToRow(mic, micRow, imgLabel=xmipp.MDL_MICROGRAPH, **kwargs)
def rowToMicrograph(micRow, **kwargs):
""" Create a Micrograph object from a row of Xmipp metadata. """
return rowToImage(micRow, xmipp.MDL_MICROGRAPH, Micrograph, **kwargs)
def volumeToRow(vol, volRow, **kwargs):
""" Set labels values from Micrograph mic to md row. """
imageToRow(vol, volRow, imgLabel=xmipp.MDL_IMAGE, writeAcquisition=False, **kwargs)
def rowToVolume(volRow, **kwargs):
""" Create a Volume object from a row of Xmipp metadata. """
return rowToImage(volRow, xmipp.MDL_IMAGE, Volume, **kwargs)
def coordinateToRow(coord, coordRow, copyId=True):
""" Set labels values from Coordinate coord to md row. """
if copyId:
setRowId(coordRow, coord)
objectToRow(coord, coordRow, COOR_DICT)
if coord.getMicId():
coordRow.setValue(xmipp.MDL_MICROGRAPH, str(coord.getMicId()))
def rowToCoordinate(coordRow):
""" Create a Coordinate from a row of a metadata. """
# Check that all required labels are present in the row
if _containsAll(coordRow, COOR_DICT):
coord = Coordinate()
rowToObject(coordRow, coord, COOR_DICT)
# Setup the micId if is integer value
try:
coord.setMicId(int(coordRow.getValue(xmipp.MDL_MICROGRAPH)))
except Exception:
pass
else:
coord = None
return coord
def _rowToParticle(partRow, particleClass, **kwargs):
""" Create a Particle from a row of a metadata. """
img = rowToImage(partRow, xmipp.MDL_IMAGE, particleClass, **kwargs)
img.setCoordinate(rowToCoordinate(partRow))
# copy micId if available
# if not copy micrograph name if available
try:
if partRow.hasLabel(xmipp.MDL_MICROGRAPH_ID):
img.setMicId(partRow.getValue(xmipp.MDL_MICROGRAPH_ID))
# elif partRow.hasLabel(xmipp.MDL_MICROGRAPH):
# micName = partRow.getValue(xmipp.MDL_MICROGRAPH)
# img._micrograph = micName
# print "setting micname as", micName
# img.printAll()
# print "getAttributes1", img._micrograph
# print "getAttributes2", getattr(img,"_micrograph",'kk')
# else:
# print "WARNING: No micname"
except Exception as e:
print "Warning:", e.message
return img
def rowToParticle(partRow, **kwargs):
return _rowToParticle(partRow, Particle, **kwargs)
def rowToMovieParticle(partRow, **kwargs):
return _rowToParticle(partRow, MovieParticle, **kwargs)
def particleToRow(part, partRow, **kwargs):
""" Set labels values from Particle to md row. """
imageToRow(part, partRow, xmipp.MDL_IMAGE, **kwargs)
coord = part.getCoordinate()
if coord is not None:
coordinateToRow(coord, partRow, copyId=False)
if part.hasMicId():
partRow.setValue(xmipp.MDL_MICROGRAPH_ID, long(part.getMicId()))
partRow.setValue(xmipp.MDL_MICROGRAPH, str(part.getMicId()))
def rowToClass(classRow, classItem):
""" Method base to create a class2D, class3D or classVol from
a row of a metadata
"""
setObjId(classItem, classRow, label=xmipp.MDL_REF)
if classRow.containsLabel(xmipp.MDL_IMAGE):
index, filename = xmippToLocation(classRow.getValue(xmipp.MDL_IMAGE))
img = classItem.REP_TYPE()
classItem.setObjId(classRow.getObjId())
# img.copyObjId(classItem)
img.setLocation(index, filename)
img.setSamplingRate(classItem.getSamplingRate())
classItem.setRepresentative(img)
return classItem
def class2DToRow(class2D, classRow):
""" Set labels values from Class2D to md row. """
if class2D.hasRepresentative():
index, filename = class2D.getRepresentative().getLocation()
fn = locationToXmipp(index, filename)
classRow.setValue(xmipp.MDL_IMAGE, fn)
n = long(len(class2D))
classRow.setValue(xmipp.MDL_CLASS_COUNT, n)
classRow.setValue(xmipp.MDL_REF, int(class2D.getObjId()))
classRow.setValue(xmipp.MDL_ITEM_ID, long(class2D.getObjId()))
def ctfModelToRow(ctfModel, ctfRow):
""" Set labels values from ctfModel to md row. """
objectToRow(ctfModel, ctfRow, CTF_DICT, extraLabels=CTF_EXTRA_LABELS)
def defocusGroupSetToRow(defocusGroup, defocusGroupRow):
""" Set labels values from ctfModel to md row. """
objectToRow(defocusGroup, defocusGroupRow, CTF_DICT)
def setPsdFiles(ctfModel, ctfRow):
""" Set the PSD files of CTF estimation related
to this ctfModel. The values will be read from
the ctfRow if present.
"""
for attr, label in CTF_PSD_DICT.iteritems():
if ctfRow.containsLabel(label):
setattr(ctfModel, attr, String(ctfRow.getValue(label)))
def rowToCtfModel(ctfRow):
""" Create a CTFModel from a row of a metadata. """
if _containsAll(ctfRow, CTF_DICT):
ctfModel = CTFModel()
rowToObject(ctfRow, ctfModel, CTF_DICT, extraLabels=CTF_EXTRA_LABELS)
ctfModel.standardize()
setPsdFiles(ctfModel, ctfRow)
else:
ctfModel = None
return ctfModel
def acquisitionToRow(acquisition, ctfRow):
""" Set labels values from acquisition to md row. """
objectToRow(acquisition, ctfRow, ACQUISITION_DICT)
def rowToAcquisition(acquisitionRow):
""" Create an acquisition from a row of a metadata. """
if _containsAll(acquisitionRow, ACQUISITION_DICT):
acquisition = Acquisition()
rowToObject(acquisitionRow, acquisition, ACQUISITION_DICT)
else:
acquisition = None
return acquisition
def readSetOfMicrographs(filename, micSet, **kwargs):
readSetOfImages(filename, micSet, rowToMicrograph, **kwargs)
def writeSetOfMicrographs(micSet, filename, blockName='Micrographs', **kwargs):
writeSetOfImages(micSet, filename, micrographToRow, blockName, **kwargs)
def readSetOfVolumes(filename, volSet, **kwargs):
readSetOfImages(filename, volSet, rowToVolume, **kwargs)
def writeSetOfVolumes(volSet, filename, blockName='Volumes', **kwargs):
writeSetOfImages(volSet, filename, volumeToRow, blockName, **kwargs)
def mdToCTFModel(md, mic):
ctfRow = rowFromMd(md, md.firstObject())
ctfObj = rowToCtfModel(ctfRow)
if md.containsLabel(xmipp.MDL_CTF_CRIT_NONASTIGMATICVALIDITY):
ctfObj._xmipp_ctfCritNonAstigmaticValidty = Float(ctfRow.getValue(xmipp.MDL_CTF_CRIT_NONASTIGMATICVALIDITY))
if md.containsLabel(xmipp.MDL_CTF_CRIT_NONASTIGMATICVALIDITY):
ctfObj._xmipp_ctfCritCtfMargin = Float(ctfRow.getValue(xmipp.MDL_CTF_CRIT_FIRSTMINIMUM_FIRSTZERO_DIFF_RATIO))
# the ctf id is set to micId when calling setMicrograph
ctfObj.setMicrograph(mic)
return ctfObj
def readCTFModel(filename, mic):
""" Read from Xmipp .ctfparam and create a CTFModel object. """
md = xmipp.MetaData(filename)
return mdToCTFModel(md, mic)
def writeSetOfCoordinates(posDir, coordSet):
""" Write a pos file on metadata format for each micrograph
on the coordSet.
Params:
posDir: the directory where the .pos files will be written.
coordSet: the SetOfCoordinates that will be read.
"""
posFiles = []
boxSize = coordSet.getBoxSize() or 100
# Write pos metadatas (one per micrograph)
for mic in coordSet.iterMicrographs():
micName = mic.getFileName()
posFn = join(posDir, replaceBaseExt(micName, "pos"))
md = xmipp.MetaData()
for coord in coordSet.iterCoordinates(micrograph=mic):
objId = md.addObject()
coordRow = XmippMdRow()
coordinateToRow(coord, coordRow)
coordRow.writeToMd(md, objId)
if not md.isEmpty():
md2 = xmipp.MetaData()
objId = md2.addObject()
md2.setValue(xmipp.MDL_PICKING_MICROGRAPH_STATE, 'Manual', objId)
# Write header block
md2.write('header@%s' % posFn)
# Write particles block
md.write('particles@%s' % posFn, xmipp.MD_APPEND)
posFiles.append(posFn)
# Write config.xmd metadata
configFn = join(posDir, 'config.xmd')
md = xmipp.MetaData()
# Write properties block
objId = md.addObject()
micName = removeBaseExt(micName)
md.setValue(xmipp.MDL_MICROGRAPH, str(micName), objId)
#md.setValue(xmipp.MDL_COLOR, int(-16776961), objId)
md.setValue(xmipp.MDL_PICKING_PARTICLE_SIZE, int(boxSize), objId)
md.setValue(xmipp.MDL_PICKING_STATE, 'Manual', objId)
md.write('properties@%s' % configFn)
# Write filters block
md = xmipp.MetaData()
objId = md.addObject()
md.setValue(xmipp.MDL_MACRO_CMD, 'Gaussian_Blur...', objId)
md.setValue(xmipp.MDL_MACRO_CMD_ARGS, 'sigma=2', objId)
md.write('filters@%s' % configFn, xmipp.MD_APPEND)
return posFiles
def readSetOfCoordinates(outputDir, micSet, coordSet):
""" Read from Xmipp .pos files.
Params:
outputDir: the directory where the .pos files are.
It is also expected a file named: config.xmd
in this directory where the box size can be read.
micSet: the SetOfMicrographs to associate the .pos, which
name should be the same of the micrographs.
coordSet: the SetOfCoordinates that will be populated.
"""
# Read the boxSize from the config.xmd metadata
configfile = join(outputDir, 'config.xmd')
if exists(configfile):
md = xmipp.MetaData('properties@' + join(outputDir, 'config.xmd'))
boxSize = md.getValue(xmipp.MDL_PICKING_PARTICLE_SIZE, md.firstObject())
coordSet.setBoxSize(boxSize)
for mic in micSet:
posFile = join(outputDir, replaceBaseExt(mic.getFileName(), 'pos'))
readCoordinates(mic, posFile, coordSet, outputDir)
coordSet._xmippMd = String(outputDir)
def readCoordinates(mic, fileName, coordsSet, outputDir):
posMd = readPosCoordinates(fileName)
posMd.addLabel(xmipp.MDL_ITEM_ID)#TODO: CHECK IF THIS LABEL IS STILL NECESSARY
for objId in posMd:
coord = rowToCoordinate(rowFromMd(posMd, objId))
coord.setMicrograph(mic)
coord.setX(coord.getX())
coord.setY(coord.getY())
coordsSet.append(coord)
# Add an unique ID that will be propagated to particles
posMd.setValue(xmipp.MDL_ITEM_ID, long(coord.getObjId()), objId)
if not posMd.isEmpty():
scipionPosFile = join(outputDir, "scipion_" + replaceBaseExt(mic.getFileName(), 'pos'))
posMd.write("particles@%s" % scipionPosFile)
def readPosCoordinates(posFile):
""" Read the coordinates in .pos file and return corresponding metadata.
There are two possible blocks with particles:
particles: with manual/supervised particles
particles_auto: with automatically picked particles.
If posFile doesn't exist, the metadata will be empty
"""
md = xmipp.MetaData()
if exists(posFile):
blocks = xmipp.getBlocksInMetaDataFile(posFile)
for b in ['particles', 'particles_auto']:
if b in blocks:
mdAux = xmipp.MetaData('%(b)s@%(posFile)s' % locals())
md.unionAll(mdAux)
md.removeDisabled()
return md
def readSetOfImages(filename, imgSet, rowToFunc, **kwargs):
"""read from Xmipp image metadata.
filename: The metadata filename where the image are.
imgSet: the SetOfParticles that will be populated.
rowToFunc: this function will be used to convert the row to Object
"""
imgMd = xmipp.MetaData(filename)
# By default remove disabled items from metadata
# be careful if you need to preserve the original number of items
if kwargs.get('removeDisabled', True):
imgMd.removeDisabled()
# If the type of alignment is not sent throught the kwargs
# try to deduced from the metadata labels
if 'alignType' not in kwargs:
imgRow = rowFromMd(imgMd, imgMd.firstObject())
if _containsAny(imgRow, ALIGNMENT_DICT):
if imgRow.containsLabel(xmipp.MDL_ANGLE_TILT):
kwargs['alignType'] = ALIGN_PROJ
else:
kwargs['alignType'] = ALIGN_2D
else:
kwargs['alignType'] = ALIGN_NONE
for objId in imgMd:
imgRow = rowFromMd(imgMd, objId)
img = rowToFunc(imgRow, **kwargs)
imgSet.append(img)
imgSet.setHasCTF(img.hasCTF())
imgSet.setAlignment(kwargs['alignType'])
def setOfImagesToMd(imgSet, md, imgToFunc, **kwargs):
""" This function will fill Xmipp metadata from a SetOfMicrographs
Params:
imgSet: the set of images to be converted to metadata
md: metadata to be filled
rowFunc: this function can be used to setup the row before
adding to metadata.
"""
if 'alignType' not in kwargs:
kwargs['alignType'] = imgSet.getAlignment()
for img in imgSet:
objId = md.addObject()
imgRow = XmippMdRow()
imgToFunc(img, imgRow, **kwargs)
imgRow.writeToMd(md, objId)
def readAnglesFromMicrographs(micFile, anglesSet):
""" Read the angles from a micrographs Metadata.
"""
micMd = xmipp.MetaData(micFile)
# micMd.removeDisabled()
for objId in micMd:
angles = Angles()
row = rowFromMd(micMd, objId)
rowToObject(row, angles, ANGLES_DICT)
angles.setObjId(micMd.getValue(xmipp.MDL_ITEM_ID, objId))
anglesSet.append(angles)
def writeSetOfImages(imgSet, filename, imgToFunc, blockName='Images', **kwargs):
""" This function will write a SetOfImages as a Xmipp metadata.
Params:
imgSet: the set of images to be written (particles, micrographs or volumes)
filename: the filename where to write the metadata.
rowFunc: this function can be used to setup the row before
adding to metadata.
"""
md = xmipp.MetaData()
setOfImagesToMd(imgSet, md, imgToFunc, **kwargs)
md.write('%s@%s' % (blockName, filename))
def readSetOfParticles(filename, partSet, **kwargs):
readSetOfImages(filename, partSet, rowToParticle, **kwargs)
def readSetOfMovieParticles(filename, partSet, **kwargs):
readSetOfImages(filename, partSet, rowToMovieParticle, **kwargs)
def setOfParticlesToMd(imgSet, md, **kwargs):
setOfImagesToMd(imgSet, md, particleToRow, **kwargs)
def setOfMicrographsToMd(imgSet, md, **kwargs):
setOfImagesToMd(imgSet, md, micrographToRow, **kwargs)
def writeSetOfParticles(imgSet, filename, blockName='Particles', **kwargs):
writeSetOfImages(imgSet, filename, particleToRow, blockName, **kwargs)
def writeCTFModel(ctfModel, ctfFile):
""" Given a CTFModel object write as Xmipp ctfparam
"""
md = xmipp.MetaData()
objId = md.addObject()
ctfRow = XmippMdRow()
ctfModelToRow(ctfModel, ctfRow)
ctfRow.writeToMd(md, objId)
md.setColumnFormat(False)
md.write(ctfFile)
def writeSetOfCTFs(ctfSet, mdCTF):
""" Write a ctfSet on metadata format.
Params:
ctfSet: the SetOfCTF that will be read.
mdCTF: The file where metadata should be written.
"""
md = xmipp.MetaData()
for ctfModel in ctfSet:
objId = md.addObject()
ctfRow = XmippMdRow()
ctfRow.setValue(xmipp.MDL_MICROGRAPH, ctfModel.getMicFile())
if ctfModel.getPsdFile():
ctfRow.setValue(xmipp.MDL_PSD, ctfModel.getPsdFile())
ctfModelToRow(ctfModel, ctfRow)
ctfRow.writeToMd(md, objId)
md.write(mdCTF)
ctfSet._xmippMd = String(mdCTF)
def writeSetOfDefocusGroups(defocusGroupSet, fnDefocusGroup): # also metadata
""" Write a defocuGroupSet on metadata format.
Params:
defocusGroupSet: the SetOfDefocus that will be read.
fnDefocusGroup: The file where defocusGroup should be written.
"""
md = xmipp.MetaData()
for defocusGroup in defocusGroupSet:
objId = md.addObject()
defocusGroupRow = XmippMdRow()
defocusGroupSetToRow(defocusGroup, defocusGroupRow)
defocusGroupRow.setValue(xmipp.MDL_CTF_GROUP, defocusGroup.getObjId())
defocusGroupRow.setValue(xmipp.MDL_MIN, defocusGroup.getDefocusMin())
defocusGroupRow.setValue(xmipp.MDL_MAX, defocusGroup.getDefocusMax())
defocusGroupRow.setValue(xmipp.MDL_AVG, defocusGroup.getDefocusAvg())
defocusGroupRow.writeToMd(md, objId)
md.write(fnDefocusGroup)
defocusGroupSet._xmippMd = String(fnDefocusGroup)
def writeSetOfClasses2D(classes2DSet, filename, classesBlock='classes', writeParticles=True):
""" This function will write a SetOfClasses2D as Xmipp metadata.
Params:
classes2DSet: the SetOfClasses2D instance.
filename: the filename where to write the metadata.
"""
classFn = '%s@%s' % (classesBlock, filename)
classMd = xmipp.MetaData()
classMd.write(classFn) # Empty write to ensure the classes is the first block
classRow = XmippMdRow()
for class2D in classes2DSet:
class2DToRow(class2D, classRow)
classRow.writeToMd(classMd, classMd.addObject())
if writeParticles:
ref = class2D.getObjId()
imagesFn = 'class%06d_images@%s' % (ref, filename)
imagesMd = xmipp.MetaData()
imgRow = XmippMdRow()
if class2D.getSize() > 0:
for img in class2D:
particleToRow(img, imgRow)
imgRow.writeToMd(imagesMd, imagesMd.addObject())
imagesMd.write(imagesFn, xmipp.MD_APPEND)
classMd.write(classFn, xmipp.MD_APPEND) # Empty write to ensure the classes is the first block
def writeSetOfMicrographsPairs(uSet, tSet, filename):
""" This function will write a MicrographsTiltPair as Xmipp metadata.
Params:
uSet: the untilted set of micrographs to be written
tSet: the tilted set of micrographs to be written
filename: the filename where to write the metadata.
"""
md = xmipp.MetaData()
for micU, micT in izip(uSet, tSet):
objId = md.addObject()
pairRow = XmippMdRow()
pairRow.setValue(xmipp.MDL_ITEM_ID, long(micU.getObjId()))
pairRow.setValue(xmipp.MDL_MICROGRAPH, micU.getFileName())
pairRow.setValue(xmipp.MDL_MICROGRAPH_TILTED, micT.getFileName())
pairRow.writeToMd(md, objId)
md.write(filename)
def readSetOfClasses(classesSet, filename, classesBlock='classes', **kwargs):
""" Read a set of classes from an Xmipp metadata with the given
convention of a block for classes and another block for each set of
images assigned to each class.
Params:
classesSet: the SetOfClasses object that will be populated.
filename: the file path where to read the Xmipp metadata.
classesBlock (by default 'classes'):
the block name of the classes group in the metadata.
"""
blocks = xmipp.getBlocksInMetaDataFile(filename)
classesMd = xmipp.MetaData('%s@%s' % (classesBlock, filename))
# Provide a hook to be used if something is needed to be
# done for special cases before converting row to class
preprocessClass = kwargs.get('preprocessClass', None)
postprocessClass = kwargs.get('postprocessClass', None)
for objId in classesMd:
classItem = classesSet.ITEM_TYPE()
classItem.setObjId(objId)
classRow = rowFromMd(classesMd, objId)
classItem = rowToClass(classRow, classItem)
# FIXME: the following is only valid for SetOfParticles
SetOfParticles.copyInfo(classItem, classesSet.getImages())
#classItem.copyInfo(classesSet.getImages())
if preprocessClass:
preprocessClass(classItem, classRow)
classesSet.append(classItem)
ref = classItem.getObjId()
b = 'class%06d_images' % ref
if b in blocks:
#FIXME: we need to adapt the following line
# when we face classes of volumes and not just particles
readSetOfParticles('%s@%s' % (b, filename), classItem, **kwargs)
if postprocessClass:
postprocessClass(classItem, classRow)
# Update with new properties of classItem such as _size
classesSet.update(classItem)
def readSetOfClasses2D(classes2DSet, filename, classesBlock='classes', **kwargs):
""" Just a wrapper to readSetOfClasses. """
readSetOfClasses(classes2DSet, filename, classesBlock, **kwargs)
def readSetOfClasses3D(classes3DSet, filename, classesBlock='classes', **kwargs):
""" Just a wrapper to readSetOfClasses. """
readSetOfClasses(classes3DSet, filename, classesBlock, **kwargs)
def writeSetOfClassesVol(classesVolSet, filename, classesBlock='classes'):
""" This function will write a SetOfClassesVol as Xmipp metadata.
Params:
classesVolSet: the SetOfClassesVol instance.
filename: the filename where to write the metadata.
"""
classFn = '%s@%s' % (classesBlock, filename)
classMd = xmipp.MetaData()
classMd.write(classFn) # Empty write to ensure the classes is the first block
classRow = XmippMdRow()
for classVol in classesVolSet:
classVolToRow(classVol, classRow)
classRow.writeToMd(classMd, classMd.addObject())
ref = class3D.getObjId()
imagesFn = 'class%06d_images@%s' % (ref, filename)
imagesMd = xmipp.MetaData()
imgRow = XmippMdRow()
for vol in classVol:
volumeToRow(vol, imgRow)
imgRow.writeToMd(imagesMd, imagesMd.addObject())
imagesMd.write(imagesFn, xmipp.MD_APPEND)
classMd.write(classFn, xmipp.MD_APPEND) # Empty write to ensure the classes is the first block