Exemple #1
0
# preprocess the data
subject_data = do_subjects_preproc(jobfile, dataset_dir=dataset_dir)[0]

# construct design matrix
nscans = len(subject_data.func[0])
frametimes = np.linspace(0, (nscans - 1) * tr, nscans)
drift_model = 'Cosine'
hrf_model = 'spm + derivative'
design_matrix = make_design_matrix(frametimes,
                                   paradigm,
                                   hrf_model=hrf_model,
                                   drift_model=drift_model,
                                   period_cut=hfcut)

# plot and save design matrix
ax = plot_design_matrix(design_matrix)
ax.set_position([.05, .25, .9, .65])
ax.set_title('Design matrix')
dmat_outfile = os.path.join(subject_data.output_dir, 'design_matrix.png')
plt.savefig(dmat_outfile, bbox_inches="tight", dpi=200)

# specify contrasts
contrasts = {}
_, matrix, names = check_design_matrix(design_matrix)
contrast_matrix = np.eye(len(names))
for i in range(len(names)):
    contrasts[names[i]] = contrast_matrix[i]

# more interesting contrasts"""
contrasts = {'active-rest': contrasts['active'] - contrasts['rest']}
Exemple #2
0
def generate_subject_stats_report(stats_report_filename,
                                  contrasts,
                                  z_maps,
                                  mask,
                                  design_matrices=None,
                                  subject_id=None,
                                  anat=None,
                                  display_mode="z",
                                  cut_coords=None,
                                  threshold=2.3,
                                  cluster_th=15,
                                  start_time=None,
                                  title=None,
                                  user_script_name=None,
                                  progress_logger=None,
                                  shutdown_all_reloaders=True,
                                  **glm_kwargs):
    """Generates a report summarizing the statistical methods and results

    Parameters
    ----------
    stats_report_filename: string:
        html file to which output (generated html) will be written

    contrasts: dict of arrays
        contrasts we are interested in; same number of contrasts as zmaps;
        same keys

    zmaps: dict of image objects or strings (image filenames)
        zmaps for contrasts we are interested in; one per contrast id

    mask: 'nifti image object'
        brain mask for ROI

    design_matrix: list of 'DesignMatrix', `numpy.ndarray` objects or of
    strings (.png, .npz, etc.) for filenames
        design matrices for the experimental conditions

    contrasts: dict of arrays
       dictionary of contrasts of interest; the keys are the contrast ids,
       the values are contrast values (lists)

    z_maps: dict of 3D image objects or strings (image filenames)
       dict with same keys as 'contrasts'; the values are paths of z-maps
       for the respective contrasts

    anat: 3D array (optional)
        brain image to serve bg unto which activation maps will be plotted

    anat_affine: 2D array (optional)
        affine data for the anat

    threshold: float (optional)
        threshold to be applied to activation maps voxel-wise

    cluster_th: int (optional)
        minimal voxel count for clusteres declared as 'activated'

    cmap: cmap object (default viz.cm.cold_hot)
        color-map to use in plotting activation maps

    start_time: string (optional)
        start time for the stats analysis (useful for the generated
        report page)

    user_script_name: string (optional, default None)
        existing filename, path to user script used in doing the analysis

    progress_logger: ProgressLogger object (optional)
        handle for logging progress

    shutdown_all_reloaders: bool (optional, default True)
        if True, all pages connected to the stats report page will
        be prevented from reloading after the stats report page
        has been completely generated

    **glm_kwargs:
        kwargs used to specify the control parameters used to specify the
        experimental paradigm and the GLM

    """
    # prepare for stats reporting
    if progress_logger is None:
        progress_logger = base_reporter.ProgressReport()

    output_dir = os.path.dirname(stats_report_filename)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # copy css and js stuff to output dir
    base_reporter.copy_web_conf_files(output_dir)

    # initialize gallery of design matrices
    design_thumbs = base_reporter.ResultsGallery(
        loader_filename=os.path.join(output_dir, "design.html"))

    # initialize gallery of activation maps
    activation_thumbs = base_reporter.ResultsGallery(
        loader_filename=os.path.join(output_dir, "activation.html"))

    # get caller module handle from stack-frame
    if user_script_name is None:
        user_script_name = sys.argv[0]
    user_source_code = base_reporter.get_module_source_code(user_script_name)

    methods = """
    GLM and Statistical Inference have been done using the <i>%s</i> script, \
powered by <a href="%s">nistats</a>.""" % (user_script_name,
                                           base_reporter.NISTATS_URL)

    # report the control parameters used in the paradigm and analysis
    design_params = ""
    glm_kwargs["contrasts"] = contrasts
    if len(glm_kwargs):
        design_params += (
            "The following control parameters were used for  "
            " specifying the experimental paradigm and fitting the "
            "GLM:<br/><ul>")

        # reshape glm_kwargs['paradigm']
        if "paradigm" in glm_kwargs:
            paradigm_ = glm_kwargs['paradigm']
            paradigm = {'name': paradigm_['name'], 'onset': paradigm_['onset']}
            if 'duration' in paradigm_.keys():
                paradigm['duration'] = paradigm_['duration']
            paradigm['n_conditions'] = len(set(paradigm['name']))
            paradigm['n_events'] = len(paradigm['name'])
            paradigm['type'] = 'event'
            if 'duration' in paradigm.keys() and paradigm['duration'][0] > 0:
                paradigm['type'] = 'block'
            glm_kwargs['paradigm'] = paradigm

        design_params += base_reporter.dict_to_html_ul(glm_kwargs)

    if start_time is None:
        start_time = base_reporter.pretty_time()

    if title is None:
        title = "GLM and Statistical Inference"
        if not subject_id is None:
            title += " for subject %s" % subject_id

    level1_html_markup = base_reporter.get_subject_report_stats_html_template(
        title=title,
        start_time=start_time,
        subject_id=subject_id,

        # insert source code stub
        source_script_name=user_script_name,
        source_code=user_source_code,
        design_params=design_params,
        methods=methods,
        threshold=threshold)

    with open(stats_report_filename, 'w') as fd:
        fd.write(str(level1_html_markup))
        fd.close()

    progress_logger.log("<b>Level 1 statistics</b><br/><br/>")

    # create design matrix thumbs
    if not design_matrices is None:
        if not hasattr(design_matrices, '__len__'):
            design_matrices = [design_matrices]

        for design_matrix, j in zip(design_matrices,
                                    range(len(design_matrices))):

            # Nistats: design matrices should be strings or pandas dataframes
            if isinstance(design_matrix, _basestring):
                if not isinstance(design_matrix, pd.DataFrame):
                    # XXX should be a DataFrame pickle here ?
                    print(design_matrix)
                    design_matrix = pd.read_pickle(design_matrix)
                else:
                    raise TypeError("Unsupported design matrix type: %s" %
                                    type(design_matrix))

            # plot design_matrix proper
            ax = plot_design_matrix(design_matrix)
            ax.set_position([.05, .25, .9, .65])
            dmat_outfile = os.path.join(output_dir,
                                        'design_matrix_%i.png' % (j + 1))
            pl.savefig(dmat_outfile, bbox_inches="tight", dpi=200)
            pl.close()

            thumb = base_reporter.Thumbnail()
            thumb.a = base_reporter.a(href=os.path.basename(dmat_outfile))
            thumb.img = base_reporter.img(src=os.path.basename(dmat_outfile),
                                          height="500px")
            thumb.description = "Design Matrix"
            thumb.description += " %s" % (
                j + 1) if len(design_matrices) > 1 else ""

            # commit activation thumbnail into gallery
            design_thumbs.commit_thumbnails(thumb)

    # create activation thumbs
    for contrast_id, contrast_val in contrasts.items():
        z_map = z_maps[contrast_id]

        # load the map
        if isinstance(z_map, _basestring):
            z_map = nibabel.load(z_map)

        # generate level 1 stats table
        title = "Level 1 stats for %s contrast" % contrast_id
        stats_table = os.path.join(output_dir,
                                   "%s_stats_table.html" % (contrast_id))
        generate_level1_stats_table(z_map,
                                    mask,
                                    stats_table,
                                    cluster_th=cluster_th,
                                    z_threshold=threshold,
                                    title=title)

        # plot activation proper
        # XXX: nilearn's plotting bug's about rotations inf affine, etc.
        z_map = reorder_img(z_map, resample="continuous")
        if not anat is None:
            anat = reorder_img(anat, resample="continuous")
        plot_stat_map(z_map,
                      anat,
                      threshold=threshold,
                      display_mode=display_mode,
                      cut_coords=cut_coords,
                      black_bg=True)

        # store activation plot
        z_map_plot = os.path.join(output_dir, "%s_z_map.png" % contrast_id)
        pl.savefig(z_map_plot,
                   dpi=200,
                   bbox_inches='tight',
                   facecolor="k",
                   edgecolor="k")
        pl.close()

        # create thumbnail for activation
        thumbnail = base_reporter.Thumbnail(tooltip="Contrast vector: %s" %
                                            contrast_val)
        thumbnail.a = base_reporter.a(href=os.path.basename(stats_table))
        thumbnail.img = base_reporter.img(
            src=os.path.basename(z_map_plot),
            height="150px",
        )
        thumbnail.description = contrast_id
        activation_thumbs.commit_thumbnails(thumbnail)

    # we're done, shut down re-loaders
    progress_logger.log('<hr/>')

    # prevent stats report page from reloading henceforth
    progress_logger.finish(stats_report_filename)

    # prevent any related page from reloading
    if shutdown_all_reloaders:
        progress_logger.finish_dir(output_dir)

    # return generated html
    with open(stats_report_filename, 'r') as fd:
        stats_report = fd.read()
        fd.close()

        return stats_report
def generate_subject_stats_report(
        stats_report_filename,
        contrasts,
        z_maps,
        mask,
        design_matrices=None,
        subject_id=None,
        anat=None,
        display_mode="z",
        cut_coords=None,
        threshold=2.3,
        cluster_th=15,
        start_time=None,
        title=None,
        user_script_name=None,
        progress_logger=None,
        shutdown_all_reloaders=True,
        **glm_kwargs):
    """Generates a report summarizing the statistical methods and results

    Parameters
    ----------
    stats_report_filename: string:
        html file to which output (generated html) will be written

    contrasts: dict of arrays
        contrasts we are interested in; same number of contrasts as zmaps;
        same keys

    zmaps: dict of image objects or strings (image filenames)
        zmaps for contrasts we are interested in; one per contrast id

    mask: 'nifti image object'
        brain mask for ROI

    design_matrix: list of 'DesignMatrix', `numpy.ndarray` objects or of
    strings (.png, .npz, etc.) for filenames
        design matrices for the experimental conditions

    contrasts: dict of arrays
       dictionary of contrasts of interest; the keys are the contrast ids,
       the values are contrast values (lists)

    z_maps: dict of 3D image objects or strings (image filenames)
       dict with same keys as 'contrasts'; the values are paths of z-maps
       for the respective contrasts

    anat: 3D array (optional)
        brain image to serve bg unto which activation maps will be plotted

    anat_affine: 2D array (optional)
        affine data for the anat

    threshold: float (optional)
        threshold to be applied to activation maps voxel-wise

    cluster_th: int (optional)
        minimal voxel count for clusteres declared as 'activated'

    cmap: cmap object (default viz.cm.cold_hot)
        color-map to use in plotting activation maps

    start_time: string (optional)
        start time for the stats analysis (useful for the generated
        report page)

    user_script_name: string (optional, default None)
        existing filename, path to user script used in doing the analysis

    progress_logger: ProgressLogger object (optional)
        handle for logging progress

    shutdown_all_reloaders: bool (optional, default True)
        if True, all pages connected to the stats report page will
        be prevented from reloading after the stats report page
        has been completely generated

    **glm_kwargs:
        kwargs used to specify the control parameters used to specify the
        experimental paradigm and the GLM

    """
    # prepare for stats reporting
    if progress_logger is None:
        progress_logger = base_reporter.ProgressReport()

    output_dir = os.path.dirname(stats_report_filename)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # copy css and js stuff to output dir
    base_reporter.copy_web_conf_files(output_dir)

    # initialize gallery of design matrices
    design_thumbs = base_reporter.ResultsGallery(
        loader_filename=os.path.join(output_dir,
                                     "design.html")
        )

    # initialize gallery of activation maps
    activation_thumbs = base_reporter.ResultsGallery(
        loader_filename=os.path.join(output_dir,
                                     "activation.html")
        )

    # get caller module handle from stack-frame
    if user_script_name is None:
        user_script_name = sys.argv[0]
    user_source_code = base_reporter.get_module_source_code(
        user_script_name)

    methods = """
    GLM and Statistical Inference have been done using the <i>%s</i> script, \
powered by <a href="%s">nistats</a>.""" % (user_script_name,
                                           base_reporter.NISTATS_URL)

    # report the control parameters used in the paradigm and analysis
    design_params = ""
    glm_kwargs["contrasts"] = contrasts
    if len(glm_kwargs):
        design_params += ("The following control parameters were used for  "
                    " specifying the experimental paradigm and fitting the "
                    "GLM:<br/><ul>")

        # reshape glm_kwargs['paradigm']
        if "paradigm" in glm_kwargs:
            paradigm_ = glm_kwargs['paradigm']
            paradigm = {'name' : paradigm_['name'],
                        'onset' : paradigm_['onset']}
            if 'duration' in paradigm_.keys():
                paradigm['duration'] = paradigm_['duration']
            paradigm['n_conditions'] = len(set(paradigm['name']))
            paradigm['n_events'] = len(paradigm['name'])
            paradigm['type'] = 'event'
            if 'duration' in paradigm.keys() and paradigm['duration'][0] > 0:
                paradigm['type'] = 'block'
            glm_kwargs['paradigm'] = paradigm

        design_params += base_reporter.dict_to_html_ul(glm_kwargs)

    if start_time is None:
        start_time = base_reporter.pretty_time()

    if title is None:
        title = "GLM and Statistical Inference"
        if not subject_id is None:
            title += " for subject %s" % subject_id

    level1_html_markup = base_reporter.get_subject_report_stats_html_template(
        title=title,
        start_time=start_time,
        subject_id=subject_id,

        # insert source code stub
        source_script_name=user_script_name,
        source_code=user_source_code,

        design_params=design_params,
        methods=methods,
        threshold=threshold)

    with open(stats_report_filename, 'w') as fd:
        fd.write(str(level1_html_markup))
        fd.close()

    progress_logger.log("<b>Level 1 statistics</b><br/><br/>")

    # create design matrix thumbs
    if not design_matrices is None:
        if not hasattr(design_matrices, '__len__'):
            design_matrices = [design_matrices]

        for design_matrix, j in zip(design_matrices,
                                    range(len(design_matrices))):
            
            # Nistats: design matrices should be strings or pandas dataframes
            if isinstance(design_matrix, basestring):
                if not isinstance(design_matrix, pd.DataFrame):
                    # XXX should be a DataFrame pickle here ?
                    print design_matrix
                    design_matrix = pd.read_pickle(design_matrix)
                else:
                    raise TypeError(
                        "Unsupported design matrix type: %s" % type(
                            design_matrix))

            # plot design_matrix proper
            ax = plot_design_matrix(design_matrix)
            ax.set_position([.05, .25, .9, .65])
            dmat_outfile = os.path.join(output_dir,
                                        'design_matrix_%i.png' % (j + 1))
            pl.savefig(dmat_outfile, bbox_inches="tight", dpi=200)
            pl.close()

            thumb = base_reporter.Thumbnail()
            thumb.a = base_reporter.a(href=os.path.basename(dmat_outfile))
            thumb.img = base_reporter.img(src=os.path.basename(dmat_outfile),
                                     height="500px")
            thumb.description = "Design Matrix"
            thumb.description += " %s" % (j + 1) if len(
                design_matrices) > 1 else ""

            # commit activation thumbnail into gallery
            design_thumbs.commit_thumbnails(thumb)

    # create activation thumbs
    for contrast_id, contrast_val in contrasts.iteritems():
        z_map = z_maps[contrast_id]

        # load the map
        if isinstance(z_map, basestring):
            z_map = nibabel.load(z_map)

        # generate level 1 stats table
        title = "Level 1 stats for %s contrast" % contrast_id
        stats_table = os.path.join(output_dir, "%s_stats_table.html" % (
                contrast_id))
        generate_level1_stats_table(
            z_map, mask, stats_table, cluster_th=cluster_th,
            z_threshold=threshold, title=title)

        # plot activation proper
        # XXX: nilearn's plotting bug's about rotations inf affine, etc.
        z_map = reorder_img(z_map, resample="continuous")
        if not anat is None:
            anat = reorder_img(anat, resample="continuous")
        plot_stat_map(z_map, anat, threshold=threshold,
                      display_mode=display_mode, cut_coords=cut_coords,
                      black_bg=True)

        # store activation plot
        z_map_plot = os.path.join(output_dir,
                                  "%s_z_map.png" % contrast_id)
        pl.savefig(z_map_plot, dpi=200, bbox_inches='tight', facecolor="k",
                   edgecolor="k")
        pl.close()

        # create thumbnail for activation
        thumbnail = base_reporter.Thumbnail(
            tooltip="Contrast vector: %s" % contrast_val)
        thumbnail.a = base_reporter.a(href=os.path.basename(stats_table))
        thumbnail.img = base_reporter.img(src=os.path.basename(z_map_plot),
                                          height="150px",)
        thumbnail.description = contrast_id
        activation_thumbs.commit_thumbnails(thumbnail)

    # we're done, shut down re-loaders
    progress_logger.log('<hr/>')

    # prevent stats report page from reloading henceforth
    progress_logger.finish(stats_report_filename)

    # prevent any related page from reloading
    if shutdown_all_reloaders:
        progress_logger.finish_dir(output_dir)

    # return generated html
    with open(stats_report_filename, 'r') as fd:
        stats_report = fd.read()
        fd.close()

        return stats_report
fd.close()

# preprocess the data
subject_data = do_subjects_preproc(jobfile, dataset_dir=dataset_dir)[0]

# construct design matrix
nscans = len(subject_data.func[0])
frametimes = np.linspace(0, (nscans - 1) * tr, nscans)
drift_model = 'Cosine'
hrf_model = 'spm + derivative'
design_matrix = make_design_matrix(
    frametimes, paradigm, hrf_model=hrf_model, drift_model=drift_model,
    period_cut=hfcut)

# plot and save design matrix
ax = plot_design_matrix(design_matrix)
ax.set_position([.05, .25, .9, .65])
ax.set_title('Design matrix')
dmat_outfile = os.path.join(subject_data.output_dir, 'design_matrix.png')
plt.savefig(dmat_outfile, bbox_inches="tight", dpi=200)

# specify contrasts
contrasts = {}
_, matrix, names = check_design_matrix(design_matrix)
contrast_matrix = np.eye(len(names))
for i in range(len(names)):
    contrasts[names[i]] = contrast_matrix[i]

# more interesting contrasts"""
contrasts = {'active-rest': contrasts['active'] - contrasts['rest']}