def generate_realignment_thumbnails( estimated_motion, output_dir, sessions=[1], execution_log_html_filename=None, results_gallery=None, progress_logger=None): """Function generates thumbnails for realignment parameters (aka estimated motion) """ if isinstance(estimated_motion, basestring): estimated_motion = [estimated_motion] output = {} for session_id, rp in zip(sessions, estimated_motion): rp_plot = os.path.join( output_dir, 'rp_plot_%s.png' % session_id) check_preprocessing.plot_spm_motion_parameters( rp, title="Plot of Estimated motion for session %s" % session_id, output_filename=rp_plot) # create thumbnail if results_gallery: thumbnail = base_reporter.Thumbnail() thumbnail.a = base_reporter.a(href=os.path.basename(rp_plot)) thumbnail.img = base_reporter.img(src=os.path.basename(rp_plot), height="250px", width="600px") thumbnail.description = "Motion Correction" if not execution_log_html_filename is None: thumbnail.description += (" (<a href=%s>see execution " "log</a>)") % os.path.basename( execution_log_html_filename) results_gallery.commit_thumbnails(thumbnail) output['rp_plot'] = rp_plot return output
def generate_ica_report( stats_report_filename, ica_maps, mask=None, report_title='ICA Report', methods_text='ICA', anat=None, anat_affine=None, threshold=2., cluster_th=0, cmap=viz.cm.cold_hot, start_time=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; passed to viz.plot_map API 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) # copy css and js stuff to output dir base_reporter.copy_web_conf_files(output_dir) # 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) if start_time is None: start_time = time.ctime() ica_html_markup = base_reporter.get_ica_html_template( ).substitute( title=report_title, start_time=start_time, # insert source code stub source_script_name=user_script_name, source_code=user_source_code, methods=methods_text, cmap=cmap.name) with open(stats_report_filename, 'w') as fd: fd.write(str(ica_html_markup)) fd.close() progress_logger.log("<b>ICA</b><br/><br/>") # make colorbar (place-holder, will be overridden, once we've figured out # the correct end points) for activations colorbar_outfile = os.path.join(output_dir, 'activation_colorbar.png') base_reporter.make_standalone_colorbar( cmap, threshold, 8., colorbar_outfile) # generate thumbs for the gallery _vmax = 0 _vmin = threshold for ica_map_id, ica_map in ica_maps.iteritems(): # load the map if isinstance(ica_map, basestring): ica_map = nibabel.load(ica_map) # compute cut_coords for viz.plot_map(..) API cut_coords = base_reporter.get_cut_coords( ica_map.get_data(), n_axials=12, delta_z_axis=3) # compute vmin and vmax vmin, vmax = base_reporter.compute_vmin_vmax(ica_map.get_data()) # update colorbar endpoints _vmax = max(_vmax, vmax) _vmin = min(_vmin, vmin) # plot activation proper viz.plot_map(ica_map.get_data(), ica_map.get_affine(), cmap=cmap, anat=anat, anat_affine=anat_affine, vmin=vmin, vmax=vmax, threshold=threshold, slicer='z', cut_coords=cut_coords, black_bg=True, ) # store activation plot ica_map_plot = os.path.join(output_dir, "%s_ica_map.png" % ica_map_id) pl.savefig(ica_map_plot, dpi=200, bbox_inches='tight', facecolor="k", edgecolor="k") stats_table = ica_map_plot # os.path.join(output_dir, # "%s_stats_table.html" % ica_map_id) # create thumbnail for activation thumbnail = base_reporter.Thumbnail() thumbnail.a = base_reporter.a(href=os.path.basename(stats_table)) thumbnail.img = base_reporter.img( src=os.path.basename(ica_map_plot), height="200px",) thumbnail.description = "Component: %s" % ica_map_id activation_thumbs.commit_thumbnails(thumbnail) # make colorbar for activations base_reporter.make_standalone_colorbar( cmap, _vmin, _vmax, colorbar_outfile) # 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
def generate_subject_stats_report( stats_report_filename, contrasts, z_maps, mask, design_matrices=None, subject_id=None, anat=None, anat_affine=None, slicer="z", cut_coords=None, statistical_mapping_trick=False, threshold=2.3, cluster_th=0, cmap=viz.cm.cold_hot, 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; passed to viz.plot_map API 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 """ # Delayed import of nipy for more robustness when it is not present from nipy.modalities.fmri.design_matrix import DesignMatrix if slicer == "ortho": statistical_mapping_trick = False if not hasattr(cut_coords, "__iter__"): cut_coords = None # 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">nipy</a>. Statistic images have been thresholded at \ Z>%s voxel-level. """ % (user_script_name, base_reporter.NIPY_URL, threshold) # 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>") 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, cmap=cmap.name) 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, xrange(len(design_matrices))): # sanitize design_matrix type if isinstance(design_matrix, basestring): if not isinstance(design_matrix, DesignMatrix): if design_matrix.endswith('.npz'): npz = np.load(design_matrix) design_matrix = DesignMatrix(npz['X'], npz['conditions'], ) else: # XXX handle case of .png, jpeg design matrix image raise TypeError( "Unsupported design matrix type: %s" % type( design_matrix)) elif isinstance(design_matrix, np.ndarray) or isinstance( design_matrix, list): X = np.array(design_matrix) # assert len(X.shape) == 2 conditions = ['%i' % i for i in xrange(X.shape[-1])] design_matrix = DesignMatrix(X, conditions) # else: # raise TypeError( # "Unsupported design matrix type '%s'" % type( # design_matrix)) # plot design_matrix proper ax = design_matrix.show(rescale=True) 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) # make colorbar (place-holder, will be overridden, once we've figured out # the correct end points) for activations colorbar_outfile = os.path.join(output_dir, 'activation_colorbar.png') base_reporter.make_standalone_colorbar( cmap, threshold, 8., colorbar_outfile) # create activation thumbs _vmax = 0 _vmin = threshold 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) # compute vmin and vmax vmin, vmax = base_reporter.compute_vmin_vmax(z_map.get_data()) # update colorbar endpoints _vmax = max(_vmax, vmax) _vmin = min(_vmin, vmin) # sanitize anat if not anat is None: if anat.ndim == 4: assert anat.shape[-1] == 1, ( "Expecting 3D array for ant, got shape %s" % str( anat.shape)) anat = anat[..., 0] else: assert anat.ndim == 3, ( "Expecting 3D array for ant, got shape %s" % str( anat.shape)) # 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)) z_clusters = generate_level1_stats_table( z_map, mask, stats_table, cluster_th=cluster_th, z_threshold=threshold, title=title, ) # compute cut_coords if statistical_mapping_trick: slicer = slicer.lower() if slicer in ["x", "y", "z"] and cut_coords is None or isinstance( cut_coords, int): axis = 'xyz'.index(slicer) if cut_coords is None: cut_coords = min(5, len(z_clusters)) _cut_coords = [x for zc in z_clusters for x in list( set(zc['maxima'][..., axis]))] # # _cut_coords = _maximally_separated_subset( # # _cut_coords, cut_coords) # assert len(_cut_coords) == cut_coords cut_coords = _cut_coords # plot activation proper viz.plot_map(z_map.get_data(), z_map.get_affine(), cmap=cmap, anat=anat, anat_affine=anat_affine, threshold=threshold, slicer=slicer, 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() 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) # make colorbar for activations base_reporter.make_standalone_colorbar( cmap, _vmin, _vmax, colorbar_outfile) # 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_ica_report(stats_report_filename, ica_maps, mask=None, report_title='ICA Report', methods_text='ICA', anat=None, anat_affine=None, threshold=2., cluster_th=0, cmap=viz.cm.cold_hot, start_time=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; passed to viz.plot_map API 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) # copy css and js stuff to output dir base_reporter.copy_web_conf_files(output_dir) # 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) if start_time is None: start_time = time.ctime() ica_html_markup = base_reporter.get_ica_html_template().substitute( title=report_title, start_time=start_time, # insert source code stub source_script_name=user_script_name, source_code=user_source_code, methods=methods_text, cmap=cmap.name) with open(stats_report_filename, 'w') as fd: fd.write(str(ica_html_markup)) fd.close() progress_logger.log("<b>ICA</b><br/><br/>") # make colorbar (place-holder, will be overridden, once we've figured out # the correct end points) for activations colorbar_outfile = os.path.join(output_dir, 'activation_colorbar.png') base_reporter.make_standalone_colorbar(cmap, threshold, 8., colorbar_outfile) # generate thumbs for the gallery _vmax = 0 _vmin = threshold for ica_map_id, ica_map in ica_maps.iteritems(): # load the map if isinstance(ica_map, basestring): ica_map = nibabel.load(ica_map) # compute cut_coords for viz.plot_map(..) API cut_coords = base_reporter.get_cut_coords(ica_map.get_data(), n_axials=12, delta_z_axis=3) # compute vmin and vmax vmin, vmax = base_reporter.compute_vmin_vmax(ica_map.get_data()) # update colorbar endpoints _vmax = max(_vmax, vmax) _vmin = min(_vmin, vmin) # plot activation proper viz.plot_map( ica_map.get_data(), ica_map.get_affine(), cmap=cmap, anat=anat, anat_affine=anat_affine, vmin=vmin, vmax=vmax, threshold=threshold, slicer='z', cut_coords=cut_coords, black_bg=True, ) # store activation plot ica_map_plot = os.path.join(output_dir, "%s_ica_map.png" % ica_map_id) pl.savefig(ica_map_plot, dpi=200, bbox_inches='tight', facecolor="k", edgecolor="k") pl.close() stats_table = ica_map_plot # os.path.join(output_dir, # "%s_stats_table.html" % ica_map_id) # create thumbnail for activation thumbnail = base_reporter.Thumbnail() thumbnail.a = base_reporter.a(href=os.path.basename(stats_table)) thumbnail.img = base_reporter.img( src=os.path.basename(ica_map_plot), height="200px", ) thumbnail.description = "Component: %s" % ica_map_id activation_thumbs.commit_thumbnails(thumbnail) # make colorbar for activations base_reporter.make_standalone_colorbar(cmap, _vmin, _vmax, colorbar_outfile) # 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_preproc_report( func=None, anat=None, estimated_motion=None, output_dir='/tmp', subject_id="UNSPECIFIED!", sessions=['UNKNOWN_SESSION'], do_cv_tc=True, preproc_undergone=None, conf_path='.', last_stage=True, parent_results_gallery=None, subject_progress_logger=None, ): output = {} # sanity if not os.path.exists(output_dir): os.makedirs(output_dir) def finalize_report(): output['final_thumbnail'] = final_thumbnail if parent_results_gallery: base_reporter.commit_subject_thumnbail_to_parent_gallery( final_thumbnail, subject_id, parent_results_gallery) if last_stage: subject_progress_logger.finish(report_preproc_filename) subject_progress_logger.finish_all() if not preproc_undergone: preproc_undergone = """\ <p>All preprocessing has been done using <a href="%s">pypreprocess</a>, which is powered by <a href="%s">nipype</a>, and <a href="%s">SPM8</a>. </p>""" % (base_reporter.PYPREPROCESS_URL, base_reporter.NIPYPE_URL, base_reporter.SPM8_URL) report_log_filename = os.path.join( output_dir, 'report_log.html') report_preproc_filename = os.path.join( output_dir, 'report_preproc.html') report_filename = os.path.join( output_dir, 'report.html') # copy css and js stuff to output dir for js_file in glob.glob(os.path.join(base_reporter.ROOT_DIR, "js/*.js")): shutil.copy(js_file, output_dir) for css_file in glob.glob(os.path.join(base_reporter.ROOT_DIR, "css/*.css")): shutil.copy(css_file, output_dir) for icon_file in glob.glob(os.path.join(base_reporter.ROOT_DIR, "icons/*.gif")): shutil.copy(icon_file, output_dir) for icon_file in glob.glob(os.path.join(base_reporter.ROOT_DIR, "images/*.png")): shutil.copy(icon_file, output_dir) for icon_file in glob.glob(os.path.join(base_reporter.ROOT_DIR, "images/*.jpeg")): shutil.copy(icon_file, output_dir) # initialize results gallery loader_filename = os.path.join( output_dir, "results_loader.php") results_gallery = base_reporter.ResultsGallery( loader_filename=loader_filename, title="Report for subject %s" % subject_id) final_thumbnail = base_reporter.Thumbnail() final_thumbnail.a = base_reporter.a(href=report_preproc_filename) final_thumbnail.img = base_reporter.img() final_thumbnail.description = subject_id output['results_gallery'] = results_gallery # initialize progress bar if subject_progress_logger is None: subject_progress_logger = base_reporter.ProgressReport( report_log_filename, other_watched_files=[report_filename, report_preproc_filename]) output['progress_logger'] = subject_progress_logger # html markup preproc = base_reporter.get_subject_report_preproc_html_template( ).substitute( conf_path=conf_path, results=results_gallery, start_time=time.ctime(), preproc_undergone=preproc_undergone, subject_id=subject_id, ) main_html = base_reporter.get_subject_report_html_template( ).substitute( conf_path=conf_path, start_time=time.ctime(), subject_id=subject_id ) with open(report_preproc_filename, 'w') as fd: fd.write(str(preproc)) fd.close() with open(report_filename, 'w') as fd: fd.write(str(main_html)) fd.close() # generate realignment thumbs if estimated_motion: generate_realignment_thumbnails( estimated_motion, output_dir, sessions=sessions, results_gallery=results_gallery, ) # generate epi normalizatipon thumbs generate_normalization_thumbnails( func, output_dir, brain="EPI", cmap=pl.cm.spectral, results_gallery=results_gallery) seg_thumbs = generate_segmentation_thumbnails( func, output_dir, cmap=pl.cm.spectral, brain="EPI", results_gallery=results_gallery, ) final_thumbnail.img.src = seg_thumbs['axial'] # generate anat normalization thumbs if anat: generate_normalization_thumbnails( anat, output_dir, brain="anat", cmap=pl.cm.gray, results_gallery=results_gallery) generate_segmentation_thumbnails( anat, output_dir, cmap=pl.cm.gray, brain="anat", results_gallery=results_gallery, ) # generate cv tc plots if do_cv_tc: generate_cv_tc_thumbnail( func, sessions, subject_id, output_dir, results_gallery=results_gallery) # we're done finalize_report() return output
def generate_cv_tc_thumbnail( image_files, sessions, subject_id, output_dir, plot_diff=True, results_gallery=None): """Generate cv tc thumbnails Parameters ---------- image_files: list or strings or list paths (4D case) to list of paths (3D case) of images under inspection output_dir: string dir to which all output whill be written subject_id: string id of subject under inspection sessions: list list of session ids, one per element of image_files result_gallery: base_reporter.ResultsGallery instance (optional) gallery to which thumbnails will be committed """ qa_cache_dir = os.path.join(output_dir, "QA") if not os.path.exists(qa_cache_dir): os.makedirs(qa_cache_dir) qa_mem = joblib.Memory(cachedir=qa_cache_dir, verbose=5) if isinstance(image_files, basestring): image_files = [image_files] else: if io_utils.is_3D(image_files[0]): image_files = [image_files] assert len(sessions) == len(image_files) cv_tc_plot_output_file = os.path.join( output_dir, "cv_tc_plot.png") qa_mem.cache( check_preprocessing.plot_cv_tc)( image_files, sessions, subject_id, _output_dir=output_dir, cv_tc_plot_outfile=cv_tc_plot_output_file, plot_diff=True) # create thumbnail thumbnail = base_reporter.Thumbnail() thumbnail.a = base_reporter.a( href=os.path.basename(cv_tc_plot_output_file)) thumbnail.img = base_reporter.img( src=os.path.basename(cv_tc_plot_output_file), height="250px", width="600px") thumbnail.description = "Coefficient of Variation (%d sessions)"\ % len(sessions) if results_gallery: results_gallery.commit_thumbnails(thumbnail) return thumbnail
def generate_segmentation_thumbnails( normalized_files, output_dir, subject_gm_file=None, subject_wm_file=None, subject_csf_file=None, brain='EPI', execution_log_html_filename=None, cmap=None, results_gallery=None, ): """Generates thumbnails after indirect normalization (segmentation + normalization) Parameters ---------- normalized_file: list paths to normalized images (3Ds or 4Ds) output_dir: string dir to which all output will be written subject_gm_file: string (optional) path to subject GM file subject_csf_file: string (optional) path to subject WM file subject_csf_file: string (optional) path to subject CSF file brain: string (optional) a short commeent/tag like 'epi', or 'anat' cmap: optional cmap (color map) to use for plots result_gallery: base_reporter.ResultsGallery instance (optional) gallery to which thumbnails will be committed """ if isinstance(normalized_files, basestring): normalized_file = normalized_files else: mean_normalized_file = os.path.join(output_dir, "%s.nii" % brain) io_utils.compute_mean_3D_image(normalized_files, output_filename=mean_normalized_file) normalized_file = mean_normalized_file output = {} # prepare for smart caching qa_cache_dir = os.path.join(output_dir, "QA") if not os.path.exists(qa_cache_dir): os.makedirs(qa_cache_dir) qa_mem = joblib.Memory(cachedir=qa_cache_dir, verbose=5) thumb_desc = "Segmentation of %s " % brain if execution_log_html_filename: thumb_desc += (" (<a href=%s>see execution " "log</a>)") % (os.path.basename( execution_log_html_filename)) # plot contours of template compartments on subject's brain template_compartments_contours = os.path.join( output_dir, "template_tmps_contours_on_%s.png" % brain) template_compartments_contours_axial = os.path.join( output_dir, "template_compartments_contours_on_%s_axial.png" % brain) qa_mem.cache(check_preprocessing.plot_segmentation)( normalized_file, GM_TEMPLATE, wm_filename=WM_TEMPLATE, csf_filename=CSF_TEMPLATE, output_filename=template_compartments_contours_axial, slicer='z', cmap=cmap, title="template TPMs") qa_mem.cache(check_preprocessing.plot_segmentation)( normalized_file, gm_filename=GM_TEMPLATE, wm_filename=WM_TEMPLATE, csf_filename=CSF_TEMPLATE, output_filename=template_compartments_contours, cmap=cmap, title=("Template GM, WM, and CSF contours on " "subject's %s") % brain) # create thumbnail if results_gallery: thumbnail = base_reporter.Thumbnail() thumbnail.a = base_reporter.a( href=os.path.basename(template_compartments_contours)) thumbnail.img = base_reporter.img( src=os.path.basename(template_compartments_contours), height="250px") thumbnail.description = thumb_desc results_gallery.commit_thumbnails(thumbnail) # plot contours of subject's compartments on subject's brain if subject_gm_file: subject_compartments_contours = os.path.join( output_dir, "subject_tmps_contours_on_subject_%s.png" % brain) subject_compartments_contours_axial = os.path.join( output_dir, "subject_tmps_contours_on_subject_%s_axial.png" % brain) qa_mem.cache(check_preprocessing.plot_segmentation)( normalized_file, subject_gm_file, wm_filename=subject_wm_file, csf_filename=subject_csf_file, output_filename=subject_compartments_contours_axial, slicer='z', cmap=cmap, title="subject TPMs") title_prefix = "Subject's GM" if subject_wm_file: title_prefix += ", WM" if subject_csf_file: title_prefix += ", and CSF" qa_mem.cache(check_preprocessing.plot_segmentation)( normalized_file, subject_gm_file, wm_filename=subject_wm_file, csf_filename=subject_csf_file, output_filename=subject_compartments_contours, cmap=cmap, title=("%s contours on " "subject's %s") % (title_prefix, brain)) # create thumbnail if results_gallery: thumbnail = base_reporter.Thumbnail() thumbnail.a = base_reporter.a( href=os.path.basename(subject_compartments_contours)) thumbnail.img = base_reporter.img( src=os.path.basename(subject_compartments_contours), height="250px") thumbnail.description = thumb_desc results_gallery.commit_thumbnails(thumbnail) output['axials'] = {} output['axial'] = template_compartments_contours_axial return output
def generate_registration_thumbnails( target, source, procedure_name, output_dir, execution_log_html_filename=None, results_gallery=None, ): output = {} # prepare for smart caching qa_cache_dir = os.path.join(output_dir, "QA") if not os.path.exists(qa_cache_dir): os.makedirs(qa_cache_dir) qa_mem = joblib.Memory(cachedir=qa_cache_dir, verbose=5) thumb_desc = procedure_name if execution_log_html_filename: thumb_desc += (" (<a href=%s>see execution" " log</a>)") % (os.path.basename( execution_log_html_filename)) # plot outline (edge map) of template on the # normalized image outline = os.path.join( output_dir, "%s_on_%s_outline.png" % (target[1], source[1])) qa_mem.cache(check_preprocessing.plot_registration)( target[0], source[0], output_filename=outline, title="Outline of %s on %s" % ( target[1], source[1], )) # create thumbnail if results_gallery: thumbnail = base_reporter.Thumbnail() thumbnail.a = base_reporter.a(href=os.path.basename(outline)) thumbnail.img = base_reporter.img( src=os.path.basename(outline), height="250px") thumbnail.description = thumb_desc results_gallery.commit_thumbnails(thumbnail) # plot outline (edge map) of the normalized image # on the SPM MNI template source, target = (target, source) outline = os.path.join( output_dir, "%s_on_%s_outline.png" % (target[1], source[1])) outline_axial = os.path.join( output_dir, "%s_on_%s_outline_axial.png" % (target[1], source[1])) qa_mem.cache(check_preprocessing.plot_registration)( target[0], source[0], output_filename=outline_axial, slicer='z', title="Outline of %s on %s" % ( target[1], source[1])) output['axial'] = outline_axial qa_mem.cache(check_preprocessing.plot_registration)( target[0], source[0], output_filename=outline, title="Outline of %s on %s" % ( target[1], source[1], )) # create thumbnail if results_gallery: thumbnail = base_reporter.Thumbnail() thumbnail.a = base_reporter.a(href=os.path.basename(outline)) thumbnail.img = base_reporter.img( src=os.path.basename(outline), height="250px") thumbnail.description = thumb_desc results_gallery.commit_thumbnails(thumbnail) return output
def generate_subject_stats_report(stats_report_filename, contrasts, z_maps, mask, design_matrices=None, subject_id=None, anat=None, anat_affine=None, slicer="z", cut_coords=None, statistical_mapping_trick=False, threshold=2.3, cluster_th=0, cmap=viz.cm.cold_hot, 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; passed to viz.plot_map API 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 """ # Delayed import of nipy for more robustness when it is not present from nipy.modalities.fmri.design_matrix import DesignMatrix if slicer == "ortho": statistical_mapping_trick = False if not hasattr(cut_coords, "__iter__"): cut_coords = None # 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">nipy</a>. Statistic images have been thresholded at \ Z>%s voxel-level. """ % (user_script_name, base_reporter.NIPY_URL, threshold) # 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>") 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, cmap=cmap.name) 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, xrange(len(design_matrices))): # sanitize design_matrix type if isinstance(design_matrix, basestring): if not isinstance(design_matrix, DesignMatrix): if design_matrix.endswith('.npz'): npz = np.load(design_matrix) design_matrix = DesignMatrix( npz['X'], npz['conditions'], ) else: # XXX handle case of .png, jpeg design matrix image raise TypeError("Unsupported design matrix type: %s" % type(design_matrix)) elif isinstance(design_matrix, np.ndarray) or isinstance( design_matrix, list): X = np.array(design_matrix) # assert len(X.shape) == 2 conditions = ['%i' % i for i in xrange(X.shape[-1])] design_matrix = DesignMatrix(X, conditions) # else: # raise TypeError( # "Unsupported design matrix type '%s'" % type( # design_matrix)) # plot design_matrix proper ax = design_matrix.show(rescale=True) 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) # make colorbar (place-holder, will be overridden, once we've figured out # the correct end points) for activations colorbar_outfile = os.path.join(output_dir, 'activation_colorbar.png') base_reporter.make_standalone_colorbar(cmap, threshold, 8., colorbar_outfile) # create activation thumbs _vmax = 0 _vmin = threshold 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) # compute vmin and vmax vmin, vmax = base_reporter.compute_vmin_vmax(z_map.get_data()) # update colorbar endpoints _vmax = max(_vmax, vmax) _vmin = min(_vmin, vmin) # sanitize anat if not anat is None: if anat.ndim == 4: assert anat.shape[-1] == 1, ( "Expecting 3D array for ant, got shape %s" % str(anat.shape)) anat = anat[..., 0] else: assert anat.ndim == 3, ( "Expecting 3D array for ant, got shape %s" % str(anat.shape)) # 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)) z_clusters = generate_level1_stats_table( z_map, mask, stats_table, cluster_th=cluster_th, z_threshold=threshold, title=title, ) # compute cut_coords if statistical_mapping_trick: slicer = slicer.lower() if slicer in ["x", "y", "z"] and cut_coords is None or isinstance( cut_coords, int): axis = 'xyz'.index(slicer) if cut_coords is None: cut_coords = min(5, len(z_clusters)) _cut_coords = [ x for zc in z_clusters for x in list(set(zc['maxima'][..., axis])) ] # # _cut_coords = _maximally_separated_subset( # # _cut_coords, cut_coords) # assert len(_cut_coords) == cut_coords cut_coords = _cut_coords # plot activation proper viz.plot_map(z_map.get_data(), z_map.get_affine(), cmap=cmap, anat=anat, anat_affine=anat_affine, threshold=threshold, slicer=slicer, 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() 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) # make colorbar for activations base_reporter.make_standalone_colorbar(cmap, _vmin, _vmax, colorbar_outfile) # 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'].to_dict('list') 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))): """ # sanitize design_matrix type if isinstance(design_matrix, basestring): if not isinstance(design_matrix, DesignMatrix): if design_matrix.endswith('.npz'): npz = np.load(design_matrix) design_matrix = DesignMatrix(npz['X'], npz['conditions']) else: # XXX handle case of .png, jpeg design matrix image raise TypeError( "Unsupported design matrix type: %s" % type( design_matrix)) elif isinstance(design_matrix, np.ndarray) or isinstance( design_matrix, list): X = np.array(design_matrix) conditions = ['%i' % i for i in range(X.shape[-1])] design_matrix = DesignMatrix(X, conditions) """ # XXX NISTATS 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