Example #1
0
def get_roi(atlas, name):
    """ Get the an roi (a nifti1 object) by <name> from <atlas>. """
    
    # Set the path for the named roi
    # and try to open it.
    path = os.path.join(roi.__path__[0], 'atlases', atlas, 'rois', name)
    try:
        nifti = read_nifti(path)

        # If name can't be loaded you may need to run
        # create_rois, tell the user that.
    except IOError, err:
        print("Could not load <name>. Try create_rois() or open_atlases()")
        raise IOError(err)
Example #2
0
def get_roi(atlas, name):
    """ Get the an roi (a nifti1 object) by <name> from <atlas>. """

    # Set the path for the named roi
    # and try to open it.
    path = os.path.join(roi.__path__[0], 'atlases', atlas, 'rois', name)
    try:
        nifti = read_nifti(path)

        # If name can't be loaded you may need to run
        # create_rois, tell the user that.
    except IOError, err:
        print("Could not load <name>. Try create_rois() or open_atlases()")
        raise IOError(err)
Example #3
0
def create_rois(atlas, base, legend):
    """ Creates ROI masks for from given <atlas> using <base> (a 4d nifit1 
    file containg all rois.  A <legend>.txt file mapping roi codes to
    names is also needed, formatted as 1:Hippocampus. """

    # setup pathing, read in the process the base
    path = os.path.join(roi.__path__[0], 'atlases')
    loni = read_nifti(os.path.join(path, atlas, base))

    # Need these for writing the rois out later
    header = loni.get_header()
    affline = loni.get_affine()
    data = loni.get_data()

    # And get then the txt file that is its legend.
    fhandle = open(os.path.join(path, legend))
    fdata = [line.strip().split(':') for line in fhandle]

    # Store the legend data in a dict keyed
    # on the ROI names.
    legend_dict = {}
    for col1, col2 in fdata:
        legend_dict.update({col2 : int(col1)})
    
    # Loop over the legend, creating and 
    # saving a nii file for each item.
    roi_path = os.path.join(path, atlas, 'rois')
    if not os.path.exists(roi_path):
        os.mkdir(roi_path)

    for key, val in legend_dict.items():
        # Mask based on v, the index for he current
        # roi then create the binary roi.
        mask = np.zeros(data.shape, dtype=np.uint8)
            ## nifti1 needs unsigned integers

        mask[data == val] = 1

        # Finally, create a nifti object and write 
        # it using the key from legend as a name.
        niftiname = key + '.nii'
        nifti = nb.Nifti1Image(mask, affline, header)
        nifti.update_header()  
            ## Not sure this does anything,
            ## but just in case

        write_nifti(nifti, os.path.join(path, atlas, 'rois', niftiname))
Example #4
0
def create_rois(atlas, base, legend):
    """ Creates ROI masks for from given <atlas> using <base> (a 4d nifit1 
    file containg all rois.  A <legend>.txt file mapping roi codes to
    names is also needed, formatted as 1:Hippocampus. """

    # setup pathing, read in the process the base
    path = os.path.join(roi.__path__[0], 'atlases')
    loni = read_nifti(os.path.join(path, atlas, base))

    # Need these for writing the rois out later
    header = loni.get_header()
    affline = loni.get_affine()
    data = loni.get_data()

    # And get then the txt file that is its legend.
    fhandle = open(os.path.join(path, legend))
    fdata = [line.strip().split(':') for line in fhandle]

    # Store the legend data in a dict keyed
    # on the ROI names.
    legend_dict = {}
    for col1, col2 in fdata:
        legend_dict.update({col2: int(col1)})

    # Loop over the legend, creating and
    # saving a nii file for each item.
    roi_path = os.path.join(path, atlas, 'rois')
    if not os.path.exists(roi_path):
        os.mkdir(roi_path)

    for key, val in legend_dict.items():
        # Mask based on v, the index for he current
        # roi then create the binary roi.
        mask = np.zeros(data.shape, dtype=np.uint8)
        ## nifti1 needs unsigned integers

        mask[data == val] = 1

        # Finally, create a nifti object and write
        # it using the key from legend as a name.
        niftiname = key + '.nii'
        nifti = nb.Nifti1Image(mask, affline, header)
        nifti.update_header()
        ## Not sure this does anything,
        ## but just in case

        write_nifti(nifti, os.path.join(path, atlas, 'rois', niftiname))
"""Combine ar* functional data in along their 4th axes.

usage: combined_func_biasbox datadir
"""
import sys
import os
from roi.pre import combine4d
from roi.io import read_nifti, write_nifti

# Process the argv
if len(sys.argv[1:]) != 1:
    raise ValueError('Only one argument allowed')
datadir = sys.argv[1]

# Name the names, then read in the data
fnames = [
    "warbiasbox0.nii", "warbiasbox1.nii", "warbiasbox2.nii", "warbiasbox3.nii",
    "warbiasbox4.nii"
]

for i, fname in enumerate(fnames):
    if not os.path.exists(os.path.join(datadir, fname)):
        print("Missing {0}".format(fname))
        fnames.pop(i)

niftis = [read_nifti(os.path.join(datadir, fname)) for fname in fnames]

# Combine the nifti objects and write the result
write_nifti(combine4d(niftis), os.path.join(datadir, "warbiasbox.nii"))
"""
import sys
import os
from roi.pre import combine4d
from roi.io import read_nifti, write_nifti

# Process the argv
if len(sys.argv[1:]) != 1:
    raise ValueError('Only one argument allowed')
datadir = sys.argv[1]

# Name the names, then read in the data
fnames = ["warpolygon0.nii", 
        "warpolygon1.nii", 
        "warpolygon2.nii", 
        "warpolygon3.nii",
        "warpolygon4.nii", 
        "warpolygon5.nii"]

# Create the niftis, remove and arn if they do not exist
for i, fname in enumerate(fnames):
    if not os.path.exists(os.path.join(datadir, fname)):
        print("Missing {0}".format(fname))
        fnames.pop(i)

niftis = [read_nifti(os.path.join(datadir, fname)) for fname in fnames]

# Combine the nifti objects and write the result
write_nifti(combine4d(niftis),
        os.path.join(datadir, "warpolygon.nii"))
# Process the argv
if len(sys.argv[1:]) != 1:
    raise ValueError('Only one argument allowed')
datadir = sys.argv[1]

# Name the names, then read in the data
fnames = [
    "warbutterfly0.nii", "warbutterfly1.nii", "warbutterfly2.nii",
    "warbutterfly3.nii", "warbutterfly4.nii", "warbutterfly5.nii",
    "warbutterfly6.nii", "warbutterfly7.nii", "warbutterfly8.nii",
    "warbutterfly9.nii"
]

for i, fname in enumerate(fnames):
    if not os.path.exists(os.path.join(datadir, fname)):
        print("Missing {0}".format(fname))
        fnames.pop(i)

# Create the niftis, remove and arn if they do not exist
niftis = []
for fname in fnames:
    try:
        niftis.append(read_nifti(os.path.join(datadir, fname)))
    except IOError:
        print("Warning: {0} not found".format(fname))
        pass
#niftis = [read_nifti(os.path.join(datadir, fname)) for fname in fnames]

# Combine the nifti objects and write the result
write_nifti(combine4d(niftis), os.path.join(datadir, "warbutterfly.nii"))
fnames = [
        "warbutterfly0.nii", 
        "warbutterfly1.nii", 
        "warbutterfly2.nii", 
        "warbutterfly3.nii",
        "warbutterfly4.nii",
        "warbutterfly5.nii",
        "warbutterfly6.nii",
        "warbutterfly7.nii",
        "warbutterfly8.nii",
        "warbutterfly9.nii"]

for i, fname in enumerate(fnames):
    if not os.path.exists(os.path.join(datadir, fname)):
        print("Missing {0}".format(fname))
        fnames.pop(i)

# Create the niftis, remove and arn if they do not exist
niftis = []
for fname in fnames:
    try:
        niftis.append(read_nifti(os.path.join(datadir, fname)))
    except IOError:
        print("Warning: {0} not found".format(fname))
        pass
#niftis = [read_nifti(os.path.join(datadir, fname)) for fname in fnames]

# Combine the nifti objects and write the result
write_nifti(combine4d(niftis),
        os.path.join(datadir, "warbutterfly.nii"))