# -*- coding: utf-8 -*-
# This software and supporting documentation are distributed by
# Institut Federatif de Recherche 49
# CEA/NeuroSpin, Batiment 145,
# 91191 Gif-sur-Yvette cedex
# France
#
# This software is governed by the CeCILL-B license under
# French law and abiding by the rules of distribution of free software.
# You can use, modify and/or redistribute the software under the
# terms of the CeCILL-B license as circulated by CEA, CNRS
# and INRIA at the following URL "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL-B license and that you accept its terms.
'''.APC (commissure coordinates) IO and other tools'''
from __future__ import absolute_import
import six
from six.moves import range
from six.moves import zip
__docformat__ = 'restructuredtext en'
from soma import aims
import types
import sys
[docs]def apcRead(filename, imagefile=None):
'''Read a .APC file
- filename: *string*
- imagefile: *string*
optional filename for the image file from which the AC/PC coordinates are
taken from. Its header may be used to recover millimeters positions from
voxels if they are not specified in the .APC file itself (for older
versions of the .APC files)
- returns: *dict*
the contents of the file as a dictionary, keys being 'ac', 'pc', 'ih'
for voxel coordinates, and 'acmm', 'pcmm', 'ihmm' for millimeters
coordinates, and optionally 'comment'.
'''
f = open(filename)
lines = f.readlines()
f.close()
apcdict = {}
ac = []
pc = []
ih = []
acm = []
pcm = []
ihm = []
for l in lines:
if l[:3] == 'AC:':
apcdict['ac'] = [int(x) for x in l.split()[1:4]]
elif l[:3] == 'PC:':
apcdict['pc'] = [int(x) for x in l.split()[1:4]]
elif l[:3] == 'IH:':
apcdict['ih'] = [int(x) for x in l.split()[1:4]]
elif l[:5] == 'ACmm:':
acm = [float(x) for x in l.split()[1:4]]
apcdict['acmm'] = acm
elif l[:5] == 'PCmm:':
pcm = [float(x) for x in l.split()[1:4]]
apcdict['pcmm'] = pcm
elif l[:5] == 'IHmm:':
ihm = [float(x) for x in l.split()[1:4]]
apcdict['ihmm'] = ihm
else:
comment = apcdict.get('comment', None)
if comment is None:
comment = []
apcdict['comment'] = comment
comment.append(l.strip())
if len(acm) == 3 and len(pcm) == 3 and len(ihm) == 3:
return apcdict
if imagefile is None:
return apcdict
if isinstqnce(imagefile, six.string_types):
f = aims.Finder()
if f.check(imagefile):
image = f.header()
else:
raise IOError('Cannot read header of: ' + filename)
else:
try:
image = imagefile.header()
except: # otherwise it is considered to already be a header
image = imagefile
if not hasattr( image, 'keys' ) \
or 'voxel_size' not in image:
image = {'voxel_size': imagefile} # directly the VS list ?
vs = image['voxel_size']
if len(acm) != 3:
acm = [ac[0] * vs[0], ac[1] * vs[1], ac[2] * vs[2]]
apcdict['acmm'] = acm
if len(pcm) != 3:
pcm = [pc[0] * vs[0], pc[1] * vs[1], pc[2] * vs[2]]
apcdict['pcmm'] = pcm
if len(ihm) != 3:
ihm = [ih[0] * vs[0], ih[1] * vs[1], ih[2] * vs[2]]
apcdict['ihmm'] = ihm
try:
apcWrite(filename, apcdict)
except:
# could not write modifications
pass
return apcdict
[docs]def apcWrite(apcdict, filename):
'''Writes a .APC file from a dictionary
'''
ac = apcdict['ac']
pc = apcdict['pc']
ih = apcdict['ih']
f = open(filename, 'w')
f.write('AC: ' + ' '.join([str(x) for x in ac]) + '\n')
f.write('PC: ' + ' '.join([str(x) for x in pc]) + '\n')
f.write('IH: ' + ' '.join([str(x) for x in ih]) + '\n')
comment = apcdict.get('comment', None)
acm = apcdict.get('acmm', None)
pcm = apcdict.get('pcmm', None)
ihm = apcdict.get('ihmm', None)
if comment is None:
comment = []
if acm and pcm and ihm:
comment = ['# The previous coordinates, used by the system, '
'are defined in voxels',
'# They stem from the following coordinates in millimeters:']
for l in comment:
f.write(l + '\n')
if acm and pcm and ihm:
f.write('ACmm: ' + ' '.join([str(x) for x in acm]) + '\n')
f.write('PCmm: ' + ' '.join([str(x) for x in pcm]) + '\n')
f.write('IHmm: ' + ' '.join([str(x) for x in ihm]) + '\n')
f.close()