Пример #1
0
def test_ghost_direction():

    import os
    import pkg_resources as p

    import numpy.testing as nt

    from qap.spatial_qc import ghost_direction
    from qap.qap_utils import load_image, load_mask

    mean_epi = p.resource_filename("qap", os.path.join(test_sub_dir, \
                                   "mean_functional.nii.gz"))

    func_brain_mask = p.resource_filename("qap", os.path.join(test_sub_dir, \
                                          "functional_brain_mask" \
                                          ".nii.gz"))

    mean_epi_data = load_image(mean_epi)
    funcmask_data = load_mask(func_brain_mask, mean_epi)

    gsr_out_x = ghost_direction(mean_epi_data, funcmask_data, "x")
    gsr_out_y = ghost_direction(mean_epi_data, funcmask_data, "y")
    gsr_out_z = ghost_direction(mean_epi_data, funcmask_data, "z")

    gsr_out_all = (gsr_out_x, gsr_out_y, gsr_out_z)

    nt.assert_almost_equal(gsr_out_all[0], -0.018987976014614105, decimal=4)
    nt.assert_almost_equal(gsr_out_all[1], 0.020795321092009544, decimal=4)
    nt.assert_almost_equal(gsr_out_all[2], 0.06708560138940811, decimal=4)
Пример #2
0
def test_ghost_direction():

    import os
    import pickle
    import pkg_resources as p
    
    from qap.spatial_qc import ghost_direction
    from qap.qap_utils import load_image, load_mask

    mean_epi = p.resource_filename("qap", os.path.join(test_sub_dir, \
                                   "rest_1", \
                                   "mean_functional", \
                                   "rest_calc_tshift_resample_volreg_" \
                                   "tstat.nii.gz"))
                                   
    func_brain_mask = p.resource_filename("qap", os.path.join(test_sub_dir, \
                                          "rest_1", \
                                          "functional_brain_mask", \
                                          "rest_calc_tshift_resample_volreg" \
                                          "_mask.nii.gz"))

    mean_epi_data = load_image(mean_epi)
    funcmask_data = load_mask(func_brain_mask, mean_epi)

    gsr_out_x = ghost_direction(mean_epi_data, funcmask_data, "x")
    gsr_out_y = ghost_direction(mean_epi_data, funcmask_data, "y")
    gsr_out_z = ghost_direction(mean_epi_data, funcmask_data, "z")

    gsr_out_all = (gsr_out_x, gsr_out_y, gsr_out_z)

    print gsr_out_all

    assert gsr_out_all == (-0.013489312, 0.016911652, 0.080058813)
def test_ghost_direction():

    import os
    import pkg_resources as p
    
    import numpy.testing as nt

    from qap.spatial_qc import ghost_direction
    from qap.qap_utils import load_image, load_mask

    mean_epi = p.resource_filename("qap", os.path.join(test_sub_dir, \
                                   "mean_functional.nii.gz"))
                                   
    func_brain_mask = p.resource_filename("qap", os.path.join(test_sub_dir, \
                                          "functional_brain_mask" \
                                          ".nii.gz"))

    mean_epi_data = load_image(mean_epi)
    funcmask_data = load_mask(func_brain_mask, mean_epi)

    gsr_out_x = ghost_direction(mean_epi_data, funcmask_data, "x")
    gsr_out_y = ghost_direction(mean_epi_data, funcmask_data, "y")
    gsr_out_z = ghost_direction(mean_epi_data, funcmask_data, "z")

    gsr_out_all = (gsr_out_x, gsr_out_y, gsr_out_z)

    nt.assert_almost_equal(gsr_out_all[0], -0.018987976014614105, decimal=4)
    nt.assert_almost_equal(gsr_out_all[1], 0.020795321092009544, decimal=4)
    nt.assert_almost_equal(gsr_out_all[2], 0.06708560138940811, decimal=4)  
Пример #4
0
def qap_functional_spatial(mean_epi,
                           func_brain_mask,
                           direction,
                           subject_id,
                           session_id,
                           scan_id,
                           site_name=None,
                           out_vox=True):

    import os
    import sys

    from qap.spatial_qc import summary_mask, snr, fber, efc, fwhm, \
        ghost_direction
    from qap.qap_utils import load_image, load_mask

    # Load the data
    anat_data = load_image(mean_epi)
    fg_mask = load_mask(func_brain_mask, mean_epi)
    bg_mask = 1 - fg_mask

    # Initialize QC
    qc = dict(subject=subject_id, session=session_id, scan=scan_id)

    if site_name:
        qc['site'] = site_name

    # FBER
    qc['fber'] = fber(anat_data, fg_mask)

    # EFC
    qc['efc'] = efc(anat_data)

    # Smoothness in voxels
    tmp = fwhm(mean_epi, func_brain_mask, out_vox=out_vox)
    qc['fwhm_x'], qc['fwhm_y'], qc['fwhm_z'], qc['fwhm'] = tmp

    # Ghosting
    if (direction == "all"):
        qc['ghost_x'] = ghost_direction(anat_data, fg_mask, "x")
        qc['ghost_y'] = ghost_direction(anat_data, fg_mask, "y")
        qc['ghost_z'] = ghost_direction(anat_data, fg_mask, "z")

    else:
        qc['ghost_%s' % direction] = ghost_direction(anat_data, fg_mask,
                                                     direction)

    # Summary Measures
    qc['fg_mean'], qc['fg_std'], qc['fg_size'] = summary_mask(
        anat_data, fg_mask)
    qc['bg_mean'], qc['bg_std'], qc['bg_size'] = summary_mask(
        anat_data, bg_mask)

    qc['snr'] = None

    # SNR
    qc['snr'] = snr(qc['fg_mean'], qc['bg_std'])
    return qc
def qap_functional_spatial(
    mean_epi, func_brain_mask, direction, subject_id, session_id, scan_id, site_name=None, out_vox=True
):

    import os
    import sys

    from qap.spatial_qc import summary_mask, snr, fber, efc, fwhm, ghost_direction
    from qap.qap_utils import load_image, load_mask

    # Load the data
    anat_data = load_image(mean_epi)
    fg_mask = load_mask(func_brain_mask, mean_epi)
    bg_mask = 1 - fg_mask

    # Initialize QC
    qc = dict()

    qc["subject"] = subject_id

    qc["session"] = session_id

    qc["scan"] = scan_id

    if site_name:
        qc["site"] = site_name

    # FBER
    qc["fber"] = fber(anat_data, fg_mask)

    # EFC
    qc["efc"] = efc(anat_data)

    # Smoothness in voxels
    tmp = fwhm(mean_epi, func_brain_mask, out_vox=out_vox)
    qc["fwhm_x"], qc["fwhm_y"], qc["fwhm_z"], qc["fwhm"] = tmp

    # Ghosting
    if direction == "all":
        qc["ghost_x"] = ghost_direction(anat_data, fg_mask, "x")
        qc["ghost_y"] = ghost_direction(anat_data, fg_mask, "y")
        qc["ghost_z"] = ghost_direction(anat_data, fg_mask, "z")

    else:
        qc["ghost_%s" % direction] = ghost_direction(anat_data, fg_mask, direction)

    # Summary Measures
    qc["fg_mean"], qc["fg_std"], qc["fg_size"] = summary_mask(anat_data, fg_mask)
    qc["bg_mean"], qc["bg_std"], qc["bg_size"] = summary_mask(anat_data, bg_mask)

    qc["snr"] = None

    # SNR
    qc["snr"] = snr(qc["fg_mean"], qc["bg_std"])

    return qc
def qap_functional_spatial(mean_epi, func_brain_mask, direction, subject_id,
                           session_id, scan_id, site_name=None, out_vox=True,
                           starter=None):
    """ Calculate the functional spatial QAP measures for a functional scan.

    - The inclusion of the starter node allows several QAP measure pipelines
      which are not dependent on one another to be executed as one pipeline.
      This allows the MultiProc Nipype plugin to efficiently manage
      resources when parallelizing.

    :type mean_epi: str
    :param mean_epi: Filepath to the mean of the functional timeseries image
                     (should be 3D).
    :type func_brain_mask: str
    :param func_brain_mask: Filepath to the binary mask defining the brain
                            within the functional image.
    :type direction: str
    :param direction: For ghost-to-signal ratio; the phase-encoding direction
                      of the image - this is often "y".
    :type subject_id: str
    :param subject_id: The participant ID.
    :type session_id: str
    :param session_id: The session ID.
    :type scan_id: str
    :param scan_id: The scan ID.
    :type site_name: str
    :param site_name: (default: None) The name of the site where the scan was
                      acquired.
    :type out_vox: bool
    :param out_vox: (default: True) For FWHM measure: output the FWHM as
                    number of voxels (otherwise as mm).
    :type starter: str
    :param starter: (default: None) If this function is being pulled into a
                    Nipype pipeline, this is the dummy input for the function
                    node.
    :rtype: dict
    :return: A dictionary mapping out the QAP measure values for the current
             participant.
    """

    from time import strftime

    import qap
    from qap.spatial_qc import summary_mask, snr, fber, efc, fwhm, \
        ghost_direction
    from qap.qap_utils import load_image, load_mask

    # Load the data
    anat_data = load_image(mean_epi)
    fg_mask = load_mask(func_brain_mask, mean_epi)
    bg_mask = 1 - fg_mask

    # FBER
    fber_out = fber(anat_data, fg_mask, bg_mask)

    # EFC
    efc_out = efc(anat_data)
    
    # Smoothness in voxels
    tmp = fwhm(mean_epi, func_brain_mask, out_vox=out_vox)
    fwhm_x, fwhm_y, fwhm_z, fwhm_out = tmp

    # Summary Measures
    fg_mean, fg_std, fg_size = summary_mask(anat_data, fg_mask)
    bg_mean, bg_std, bg_size = summary_mask(anat_data, bg_mask)

    # SNR
    snr_out = snr(fg_mean, bg_std)

    id_string = "%s %s %s" % (subject_id, session_id, scan_id)
    qc = {
            id_string:
            {
               "QAP_pipeline_id": "QAP version %s" % qap.__version__,
               "Time": strftime("%Y-%m-%d %H:%M:%S"),
               "Participant": str(subject_id),
               "Session": str(session_id),
               "Series": str(scan_id),
               "functional_spatial":
               {
                  "FBER": fber_out,
                  "EFC": efc_out,
                  "FWHM": fwhm_out,
                  "FWHM_x": fwhm_x,
                  "FWHM_y": fwhm_y,
                  "FWHM_z": fwhm_z,
                  "SNR": snr_out
               }
            }
        }

    # Ghosting
    if (direction == "all"):
        qc[id_string]["functional_spatial"]['Ghost_x'] = \
            ghost_direction(anat_data, fg_mask, "x")
        qc[id_string]["functional_spatial"]['Ghost_y'] = \
            ghost_direction(anat_data, fg_mask, "y")
        qc[id_string]["functional_spatial"]['Ghost_z'] = \
            ghost_direction(anat_data, fg_mask, "z")
    else:
        qc[id_string]["functional_spatial"]['Ghost_%s' % direction] = \
            ghost_direction(anat_data, fg_mask, direction)

    if site_name:
        qc[id_string]['Site'] = str(site_name)

    for key in qc[id_string]["functional_spatial"].keys():
        qc[id_string]["functional_spatial"][key] = \
            str(qc[id_string]["functional_spatial"][key])

    return qc
Пример #7
0
def qap_functional_spatial(mean_epi,
                           func_brain_mask,
                           direction,
                           subject_id,
                           session_id,
                           scan_id,
                           site_name=None,
                           out_vox=True,
                           starter=None):
    """ Calculate the functional spatial QAP measures for a functional scan.

    - The inclusion of the starter node allows several QAP measure pipelines
      which are not dependent on one another to be executed as one pipeline.
      This allows the MultiProc Nipype plugin to efficiently manage
      resources when parallelizing.

    :type mean_epi: str
    :param mean_epi: Filepath to the mean of the functional timeseries image
                     (should be 3D).
    :type func_brain_mask: str
    :param func_brain_mask: Filepath to the binary mask defining the brain
                            within the functional image.
    :type direction: str
    :param direction: For ghost-to-signal ratio; the phase-encoding direction
                      of the image - this is often "y".
    :type subject_id: str
    :param subject_id: The participant ID.
    :type session_id: str
    :param session_id: The session ID.
    :type scan_id: str
    :param scan_id: The scan ID.
    :type site_name: str
    :param site_name: (default: None) The name of the site where the scan was
                      acquired.
    :type out_vox: bool
    :param out_vox: (default: True) For FWHM measure: output the FWHM as
                    number of voxels (otherwise as mm).
    :type starter: str
    :param starter: (default: None) If this function is being pulled into a
                    Nipype pipeline, this is the dummy input for the function
                    node.
    :rtype: dict
    :return: A dictionary mapping out the QAP measure values for the current
             participant.
    """

    from time import strftime

    import qap
    from qap.spatial_qc import summary_mask, snr, fber, efc, fwhm, \
        ghost_direction
    from qap.qap_utils import load_image, load_mask

    # Load the data
    anat_data = load_image(mean_epi)
    fg_mask = load_mask(func_brain_mask, mean_epi)
    bg_mask = 1 - fg_mask

    # FBER
    fber_out = fber(anat_data, fg_mask, bg_mask)

    # EFC
    efc_out = efc(anat_data)

    # Smoothness in voxels
    tmp = fwhm(mean_epi, func_brain_mask, out_vox=out_vox)
    fwhm_x, fwhm_y, fwhm_z, fwhm_out = tmp

    # Summary Measures
    fg_mean, fg_std, fg_size = summary_mask(anat_data, fg_mask)
    bg_mean, bg_std, bg_size = summary_mask(anat_data, bg_mask)

    # SNR
    snr_out = snr(fg_mean, bg_std)

    id_string = "%s %s %s" % (subject_id, session_id, scan_id)
    qc = {
        id_string: {
            "QAP_pipeline_id": "QAP version %s" % qap.__version__,
            "Time": strftime("%Y-%m-%d %H:%M:%S"),
            "Participant": str(subject_id),
            "Session": str(session_id),
            "Series": str(scan_id),
            "functional_spatial": {
                "FBER": fber_out,
                "EFC": efc_out,
                "FWHM": fwhm_out,
                "FWHM_x": fwhm_x,
                "FWHM_y": fwhm_y,
                "FWHM_z": fwhm_z,
                "SNR": snr_out
            }
        }
    }

    # Ghosting
    if (direction == "all"):
        qc[id_string]["functional_spatial"]['Ghost_x'] = \
            ghost_direction(anat_data, fg_mask, "x")
        qc[id_string]["functional_spatial"]['Ghost_y'] = \
            ghost_direction(anat_data, fg_mask, "y")
        qc[id_string]["functional_spatial"]['Ghost_z'] = \
            ghost_direction(anat_data, fg_mask, "z")
    else:
        qc[id_string]["functional_spatial"]['Ghost_%s' % direction] = \
            ghost_direction(anat_data, fg_mask, direction)

    if site_name:
        qc[id_string]['Site'] = str(site_name)

    for key in qc[id_string]["functional_spatial"].keys():
        qc[id_string]["functional_spatial"][key] = \
            str(qc[id_string]["functional_spatial"][key])

    return qc