Example #1
0
def condense_array(field_dict, field_base):
    field_keys = field_dict.keys()
    arrays_dict = odict()
    n = 0
    max_idx = len(field_keys)
    # advance to first array entry
    while (n < max_idx and field_keys[n].find(field_base) < 0):
        n += 1
    if n == max_idx:
        return arrays_dict
    # initialize all the arrays present.. do this by creating lists
    # for every new field where the index is 0
    idx = 0
    while (True):
        field = field_keys[n]
        if (field.find(field_base) < 0):
            break
        # field is like this: 'sSliceArray.asSlice[0].sPosition.dTra'
        # it gets decomposed into tail: '0].sPosition.dTra'
        tail = field.split('[')[-1]
        # then decomposed into the number and name: ['0', '.sPosition.dTra']
        idx_str, a_name = tail.split(']')
        idx = int(idx_str)
        if idx == 0:
            arrays_dict[a_name[1:]] = [field_dict[field]]
        else:
            arrays_dict[a_name[1:]].append(field_dict[field])
        n += 1
    for a_name, a in arrays_dict.items():
        arrays_dict[a_name] = np.array(a)
    return arrays_dict
Example #2
0
def strip_ascconv(hdr_str):

    # scan forward to the 2nd 'ASCCONV BEGIN' string
    pos = hdr_str.find('ASCCONV BEGIN')
    ##     hdr_str = hdr_str[pos+100:] # make sure to clear the 'ASCCONV' stuff
    ##     pos = hdr_str.find('ASCCONV BEGIN')
    hdr_str = hdr_str[pos:]
    # get to the first line of the ASCCONV section
    pos = hdr_str.find('\n')
    hdr_str = hdr_str[pos + 1:]
    f = cStringIO.StringIO(hdr_str)
    field_dict = odict()
    for line in f.readlines():
        if line.find('ASCCONV END') >= 0:
            break
        line = line.strip('\n')
        field, val = line.split('=')
        field = field.strip()
        val = val.strip()
        val = val.split(' #')[0]  # there may be comments like on this line??
        if (val.find('"') >= 0):
            field_dict[field] = val.strip('"')
        elif (val.find('0x') >= 0):
            field_dict[field] = int(val, 16)
        else:
            field_dict[field] = float(val)
    return field_dict
Example #3
0
def condense_array(field_dict, field_base):
    field_keys = field_dict.keys()
    arrays_dict = odict()
    n = 0
    max_idx = len(field_keys)
    # advance to first array entry
    while(n < max_idx and field_keys[n].find(field_base) < 0):
        n += 1
    if n==max_idx:
        return arrays_dict
    # initialize all the arrays present.. do this by creating lists
    # for every new field where the index is 0
    idx = 0
    while(True):
        field = field_keys[n]
        if(field.find(field_base) < 0):
            break
        # field is like this: 'sSliceArray.asSlice[0].sPosition.dTra'
        # it gets decomposed into tail: '0].sPosition.dTra'
        tail = field.split('[')[-1]
        # then decomposed into the number and name: ['0', '.sPosition.dTra']
        idx_str, a_name = tail.split(']')
        idx = int(idx_str)
        if idx==0:
            arrays_dict[a_name[1:]] = [field_dict[field]]
        else:
            arrays_dict[a_name[1:]].append(field_dict[field])
        n += 1
    for a_name, a in arrays_dict.items():
        arrays_dict[a_name] = np.array(a)
    return arrays_dict
Example #4
0
def strip_ascconv(hdr_str):

    # scan forward to the 2nd 'ASCCONV BEGIN' string
    pos = hdr_str.find('ASCCONV BEGIN')
##     hdr_str = hdr_str[pos+100:] # make sure to clear the 'ASCCONV' stuff
##     pos = hdr_str.find('ASCCONV BEGIN')
    hdr_str = hdr_str[pos:]
    # get to the first line of the ASCCONV section
    pos = hdr_str.find('\n')
    hdr_str = hdr_str[pos+1:]
    f = cStringIO.StringIO(hdr_str)
    field_dict = odict()
    for line in f.readlines():
        if line.find('ASCCONV END') >= 0:
            break
        line = line.strip('\n')
        field, val = line.split('=')
        field = field.strip()
        val = val.strip()
        val = val.split(' #')[0] # there may be comments like on this line??
        if(val.find('"') >= 0):
            field_dict[field] = val.strip('"')
        elif(val.find('0x') >= 0):
            field_dict[field] = int(val, 16)
        else:
            field_dict[field] = float(val)
    return field_dict
Example #5
0
from ConfigParser import SafeConfigParser
from optparse import OptionParser
from recon.odict import odict
from recon import import_from

tool_map = odict(
    (("dumpheader", "DumpHeader"), ("fdf2img", "Fdf2Img"),
     ("getparam", "GetParam"), ("recon-doc", "ImagingDoc"), ("recon", "Recon"),
     ("recon_gui", "ReconGUI"), ("viewimage", "ViewImageTool")))
tool_names = tool_map.keys()

default_logfile = "recon.log"


#-----------------------------------------------------------------------------
def getToolByName(toolname):
    if not tool_map.has_key(toolname):
        raise ValueError("No tool called '%s'." % toolname)
    classname = tool_map[toolname]
    modulename = "recon.tools.%s" % classname
    try:
        return import_from(modulename, classname)
    except ImportError:
        raise RuntimeError("Tool class %s.%s not found." %
                           (modulename, classname))


def parseVolRangeOption(vol_range, parser):
    """
    Separates out the command-line option volume range into distinct numbers
    @param vol_range: volume range as x:y
Example #6
0
import numpy as np
from os import path
from recon.odict import odict
from recon import import_from
from recon.inplane_xforms import compose_xform
from recon.util import Quaternion, integer_ranges, scale_data

# module-private dict specifying available image readers
_readers = odict((
    ("analyze", ("recon.analyze","readImage")),
    ("nifti", ("recon.nifti","readImage")),
    ("fid", ("recon.scanners.varian","FidImage")),
    ("fdf", ("recon.scanners.varian","FDFImage")),
    ("siemens", ("recon.scanners.siemens", "SiemensImage"))))
available_readers = _readers.keys()

# module-private dict specifying available image writers
_writers = odict((
    ("analyze", ("recon.analyze","writeImage")),
    ("nifti-single", ("recon.nifti","writeImage")),
    ("nifti-dual", ("recon.nifti","writeDualImage")),
    ))

available_writers = _writers.keys()

recon_output2dtype = odict((
    ('magnitude', np.dtype(np.float32)),
    ('complex', np.dtype(np.complex64)),
    ('double', np.dtype(np.float64)),
    ('byte', np.dtype(np.int8)),
    ('ubyte', np.dtype(np.uint8)),
Example #7
0
    </menu>
  </menubar>
</ui>'''

cmap_lookup = odict((
    (0, P.cm.Blues),
    (1, P.cm.Greens),
    (2, P.cm.Greys),
    (3, P.cm.Oranges),
    (4, P.cm.Purples),
    (5, P.cm.Reds),
    (6, P.cm.Spectral),
    (7, P.cm.autumn),
    (8, P.cm.bone),
    (9, P.cm.cool),
    (10, P.cm.copper),
    (11, P.cm.gist_earth),
    (12, P.cm.gist_gray),
    (13, P.cm.gist_heat),
    (14, P.cm.gist_rainbow),
    (15, P.cm.gray),
    (16, P.cm.hot),
    (17, P.cm.hsv),
    (18, P.cm.jet),
    (19, P.cm.spring),
    (20, P.cm.summer),
    (21, P.cm.winter),
))

interp_types = ['nearest', 'bilinear', 'sinc']
interp_lookup = odict([(num, name) for num, name in enumerate(interp_types)])
Example #8
0
import numpy as np
from os import path
from recon.odict import odict
from recon import import_from
from recon.inplane_xforms import compose_xform
from recon.util import Quaternion, integer_ranges, scale_data

# module-private dict specifying available image readers
_readers = odict(
    (("analyze", ("recon.analyze", "readImage")), ("nifti", ("recon.nifti",
                                                             "readImage")),
     ("fid", ("recon.scanners.varian",
              "FidImage")), ("fdf", ("recon.scanners.varian", "FDFImage")),
     ("siemens", ("recon.scanners.siemens", "SiemensImage"))))
available_readers = _readers.keys()

# module-private dict specifying available image writers
_writers = odict((
    ("analyze", ("recon.analyze", "writeImage")),
    ("nifti-single", ("recon.nifti", "writeImage")),
    ("nifti-dual", ("recon.nifti", "writeDualImage")),
))

available_writers = _writers.keys()

recon_output2dtype = odict((
    ('magnitude', np.dtype(np.float32)),
    ('complex', np.dtype(np.complex64)),
    ('double', np.dtype(np.float64)),
    ('byte', np.dtype(np.int8)),
    ('ubyte', np.dtype(np.uint8)),
Example #9
0
    </menu>
  </menubar>
</ui>'''

cmap_lookup = odict((
    (0, P.cm.Blues),
    (1, P.cm.Greens),
    (2, P.cm.Greys),
    (3, P.cm.Oranges),
    (4, P.cm.Purples),
    (5, P.cm.Reds),
    (6, P.cm.Spectral),
    (7, P.cm.autumn),
    (8, P.cm.bone),
    (9, P.cm.cool),
    (10, P.cm.copper),
    (11, P.cm.gist_earth),
    (12, P.cm.gist_gray),
    (13, P.cm.gist_heat),
    (14, P.cm.gist_rainbow),
    (15, P.cm.gray),
    (16, P.cm.hot),
    (17, P.cm.hsv),
    (18, P.cm.jet),
    (19, P.cm.spring),
    (20, P.cm.summer),
    (21, P.cm.winter),
    ))

interp_types = ['nearest', 'bilinear', 'sinc']
interp_lookup = odict([(num,name) for num,name in enumerate(interp_types)])
Example #10
0
byteorders = {
    sys.byteorder: sys.byteorder == 'little' and LITTLE_ENDIAN or BIG_ENDIAN,
    'swapped': sys.byteorder == 'little' and BIG_ENDIAN or LITTLE_ENDIAN
}

HEADER_SIZE = 348
struct_fields = odict(
    (('sizeof_hdr', 'i'), ('data_type', '10s'), ('db_name', '18s'),
     ('extents', 'i'), ('session_error', 'h'), ('regular', 'c'), ('hkey_un0',
                                                                  'c'),
     ('ndim', 'h'), ('idim', 'h'), ('jdim', 'h'), ('kdim', 'h'), ('tdim', 'h'),
     ('dim5', 'h'), ('dim6', 'h'), ('dim7', 'h'), ('vox_units', '4s'),
     ('cal_units', '8s'), ('unused1', 'h'), ('datatype', 'h'), ('bitpix', 'h'),
     ('dim_un0', 'h'), ('pixdim0', 'f'), ('isize', 'f'), ('jsize', 'f'),
     ('ksize', 'f'), ('tsize', 'f'), ('pixdim5', 'f'), ('pixdim6', 'f'),
     ('pixdim7', 'f'), ('vox_offset', 'f'), ('scale_factor', 'f'),
     ('funused2', 'f'), ('funused3', 'f'), ('cal_max', 'f'), ('cal_min', 'f'),
     ('compressed', 'i'), ('verified', 'i'), ('glmax', 'i'), ('glmin', 'i'),
     ('descrip', '80s'), ('aux_file', '24s'), ('orient', 'B'), ('x0', 'h'),
     ('y0', 'h'), ('z0', 'h'), ('sunused', '4s'), ('generated', '10s'),
     ('scannum', '10s'), ('patient_id', '10s'), ('exp_date',
                                                 '10s'), ('exp_time', '10s'),
     ('hist_un0', '3s'), ('views', 'i'), ('vols_added', 'i'), ('start_field',
                                                               'i'),
     ('field_skip', 'i'), ('omax', 'i'), ('omin', 'i'), ('smax', 'i'), ('smin',
                                                                        'i')))

field_formats = struct_fields.values()


##############################################################################
class AnalyzeReader:
Example #11
0
struct_fields = odict((
    ('sizeof_hdr','i'),
    ('data_type','10s'),
    ('db_name','18s'),
    ('extents','i'),
    ('session_error','h'),
    ('regular','c'),
    ('dim_info','B'),
    # these are short dim[8]
    ('ndim','h'),
    ('idim','h'),
    ('jdim','h'),
    ('kdim','h'),
    ('tdim','h'),
    ('dim5','h'),
    ('dim6','h'),
    ('dim7','h'),
    ('intent_p1','f'),
    ('intent_p2','f'),
    ('intent_p3','f'),
    ('intent_code','h'),
    ('datatype', 'h'),
    ('bitpix','h'),
    ('slice_start', 'h'),
    # these 8 float are float pixdim[8] (pixdim[0] encodes qfac)
    ('qfac','f'),
    ('isize','f'),
    ('jsize','f'),
    ('ksize','f'),
    ('tsize','f'),
    ('pixdim5','f'),
    ('pixdim6','f'),
    ('pixdim7','f'),
    ('vox_offset','f'),
    ('scl_slope','f'),
    ('scl_inter','f'),
    ('slice_end','h'),
    ('slice_code','B'),
    ('xyzt_units','B'),
    ('cal_max','f'),
    ('cal_min','f'),
    ('slice_duration','f'),
    ('toffset','f'),
    ('glmax','i'),
    ('glmin','i'),
    ('descrip','80s'),
    ('aux_file','24s'),
    ('qform_code','h'),
    ('sform_code','h'),
    ('quatern_b','f'),
    ('quatern_c','f'),
    ('quatern_d','f'),
    ('qoffset_x','f'),
    ('qoffset_y','f'),
    ('qoffset_z','f'),
    ('srow_x0','f'),
    ('srow_x1','f'),
    ('srow_x2','f'),
    ('srow_x3','f'),
    ('srow_y0','f'),
    ('srow_y1','f'),
    ('srow_y2','f'),
    ('srow_y3','f'),
    ('srow_z0','f'),
    ('srow_z1','f'),
    ('srow_z2','f'),
    ('srow_z3','f'),
    ('intent_name','16s'),
    ('magic','4s'),
))
Example #12
0
struct_fields = odict(
    (
        ("sizeof_hdr", "i"),
        ("data_type", "10s"),
        ("db_name", "18s"),
        ("extents", "i"),
        ("session_error", "h"),
        ("regular", "c"),
        ("hkey_un0", "c"),
        ("ndim", "h"),
        ("idim", "h"),
        ("jdim", "h"),
        ("kdim", "h"),
        ("tdim", "h"),
        ("dim5", "h"),
        ("dim6", "h"),
        ("dim7", "h"),
        ("vox_units", "4s"),
        ("cal_units", "8s"),
        ("unused1", "h"),
        ("datatype", "h"),
        ("bitpix", "h"),
        ("dim_un0", "h"),
        ("pixdim0", "f"),
        ("isize", "f"),
        ("jsize", "f"),
        ("ksize", "f"),
        ("tsize", "f"),
        ("pixdim5", "f"),
        ("pixdim6", "f"),
        ("pixdim7", "f"),
        ("vox_offset", "f"),
        ("scale_factor", "f"),
        ("funused2", "f"),
        ("funused3", "f"),
        ("cal_max", "f"),
        ("cal_min", "f"),
        ("compressed", "i"),
        ("verified", "i"),
        ("glmax", "i"),
        ("glmin", "i"),
        ("descrip", "80s"),
        ("aux_file", "24s"),
        ("orient", "B"),
        ("x0", "h"),
        ("y0", "h"),
        ("z0", "h"),
        ("sunused", "4s"),
        ("generated", "10s"),
        ("scannum", "10s"),
        ("patient_id", "10s"),
        ("exp_date", "10s"),
        ("exp_time", "10s"),
        ("hist_un0", "3s"),
        ("views", "i"),
        ("vols_added", "i"),
        ("start_field", "i"),
        ("field_skip", "i"),
        ("omax", "i"),
        ("omin", "i"),
        ("smax", "i"),
        ("smin", "i"),
    )
)
Example #13
0
from ConfigParser import SafeConfigParser
from optparse import OptionParser
from recon.odict import odict
from recon import import_from

tool_map = odict(
    (
        ("dumpheader", "DumpHeader"),
        ("fdf2img", "Fdf2Img"),
        ("getparam", "GetParam"),
        ("recon-doc", "ImagingDoc"),
        ("recon", "Recon"),
        ("recon_gui", "ReconGUI"),
        ("viewimage", "ViewImageTool"),
    )
)
tool_names = tool_map.keys()

default_logfile = "recon.log"
# -----------------------------------------------------------------------------
def getToolByName(toolname):
    if not tool_map.has_key(toolname):
        raise ValueError("No tool called '%s'." % toolname)
    classname = tool_map[toolname]
    modulename = "recon.tools.%s" % classname
    try:
        return import_from(modulename, classname)
    except ImportError:
        raise RuntimeError("Tool class %s.%s not found." % (modulename, classname))