RegionalTacFigure#
- class petpal.visualizations.tac_plots.RegionalTacFigure(tacs_dir: str, figsize: tuple = (8, 4), xlabel: str = '$t$ [minutes]', ylabel: str = 'TAC [$\\mathrm{kBq/ml}$]', plot_type='both')#
Bases:
TacFigure,petpal.utils.time_activity_curve.MultiTACAnalysisMixinHandle plotting regional TACs generated with PETPAL. Used when visualizing activity in several brain regions in the same participant, especially after running PETPAL’s
write_tacs()orSgtmmethods.Example:
from petpal.visualizations.tac_plots import RegionalTacFigure from petpal.utils.time_activity_curve import TimeActivityCurve ## Load up an individual TAC file my_tac = TimeActivityCurve.from_tsv("/path/to/tac.tsv") ## Select a list of regions to plot and the folder containing TAC files for those regions my_regions = ['Putamen', 'Cerebellum', 'CorticalGrayMatter'] my_fig = tac_plots.RegionalTacFigure(tacs_dir="/path/to/tacs/folder/") ## Add the individual TAC and the regional TACs to the figure my_fig.add_errorbar(*my_tac.tac_werr, label='Whole Brain') my_fig.plot_tacs_in_regions_list(regions=my_regions) ## Write the figure to file my_fig.write_fig(out_fig_path="/path/to/plot.png") plt.show() # Comment this out if you don't want to see the figure # plt.close() # Uncomment this if you don't want to see the figure
Initialize the TacFigure with two subplots, one with a linear scale and the other with a semi-logarithmic scale.
- Parameters:
figsize (tuple) – The total size of the figure. Defaults to an 8x4 inches figure.
xlabel (str) – The label for the x-axis. Defaults to ‘$t$ [minutes]’.
ylabel (str) – The label for the y-axis. Defaults to ‘TAC [$mathrm{kBq/ml}$]’.
plot_type (str) – Type of plot, with options ‘linear’, ‘log’, or ‘both’.
- property tacs_objects_dict#
A dictionary containing region name/TAC object pairs for all TAC files found in tacs_dir.
- plot_tacs_in_regions_list(regions: list[str], show_legend: bool = True, colormap: str = 'Dark2', **kwargs)#
Plot TACs for a list of provided regions without errorbars.
Region names correspond to abbreviated segment names in the dseg file used to generate the regions.
- Parameters:
regions (list[str]) – A list of region names whose TACs are plotted.
show_legend (bool) – Show the legend with region names in the resulting figure. Default True.
colormap (str) – A matplotlib color map used to select colors of different TAC plots. Default ‘Dark2’.
kwargs (dict) – Additional keyword arguments for the plt.plot() function.
- plot_tacs_in_regions_list_with_errorbar(regions: list[str], show_legend: bool = True, colormap: str = 'Dark2', **kwargs)#
Plot TACs for a list of provided regions with errorbars.
Region names correspond to abbreviated segment names in the dseg file used to generate the regions.
- Parameters:
regions (list[str]) – A list of region names whose TACs are plotted.
show_legend (bool) – Show the legend with region names in the resulting figure. Default True.
colormap (str) – A matplotlib color map used to select colors of different TAC plots. Default ‘Dark2’.
kwargs (dict) – Additional keyword arguments for the plt.errorbar() function.
- plot_all_regional_tacs(show_legend: bool = True, colormap='Dark2', **kwargs)#
Plot TACs for all TACs found in a folder without errorbars.
Region names correspond to abbreviated segment names in the dseg file used to generate the regions.
- Parameters:
show_legend (bool) – Show the legend with region names in the resulting figure. Default True.
colormap (str) – A matplotlib color map used to select colors of different TAC plots. Default ‘Dark2’.
kwargs (dict) – Additional keyword arguments for the plt.plot() function.
- plot_all_regional_tacs_with_errorbar(show_legend: bool = True, colormap='Dark2', **kwargs)#
Plot TACs for all TACs found in a folder with errorbars.
Region names correspond to abbreviated segment names in the dseg file used to generate the regions.
- Parameters:
show_legend (bool) – Show the legend with region names in the resulting figure. Default True.
colormap (str) – A matplotlib color map used to select colors of different TAC plots. Default ‘Dark2’.
kwargs (dict) – Additional keyword arguments for the plt.errorbar() function.
- setup_linear_subplot(xlabel: str, ylabel: str, figsize: tuple)#
Get the figure and axes objects for a 1x1 MatPlotLib subplot.
- Parameters:
xlabel (str) – The label for the x-axis.
ylabel (str) – The label for the y-axis.
figsize (tuple) – Size of the figure.
- setup_log_subplot(xlabel: str, ylabel: str, figsize: tuple)#
Get the figure and axes objects for a 1x1 MatPlotLib subplot.
- Parameters:
xlabel (str) – The label for the x-axis.
ylabel (str) – The label for the y-axis.
figsize (tuple) – Size of the figure.
- setup_linear_and_log_subplot(xlabel: str, ylabel: str, figsize: tuple)#
Get the figure and axes objects for a 1x2 MatPlotLib subplot.
- Parameters:
xlabel (str) – The label for the x-axis.
ylabel (str) – The label for the y-axis.
figsize (tuple) – Size of the figure.
- add_tac(tac_times: numpy.ndarray, tac_vals: numpy.ndarray, **kwargs)#
Add a TAC to both subplots.
- Parameters:
tac_times (np.ndarray) – The time points for the TAC.
tac_vals (np.ndarray) – The corresponding values for the TAC.
kwargs (dict) – Additional keyword arguments for the plot() function.
- add_errorbar(tac_times: numpy.ndarray, tac_vals: numpy.ndarray, uncertainty: numpy.ndarray, **kwargs)#
Add a lineplot with errorbars to the figure.
- Parameters:
tac_times (np.ndarray) – Array containing times at which TAC is sampled.
tac_vals (np.ndarray) – Array containing TAC activity concentration.
uncertainty (np.ndarray) – Array containing uncertainties in TAC measurements.
kwargs (dict) – Additional keyword arguments for the plt.errorbar() function.
- set_ylim_min_to_zero(**kwargs)#
Set the y-axis lower limit to zero.
- Parameters:
kwargs (dict) – Additional keyword arguments for the set_ylim() function.
- gen_legend()#
Generate a legend using the labels provided in the add_tac() method.
Note
It is recommended to add all TACs before generating the legend. Any TACs added after
the legend is generated will not be included in the legend.
- write_fig(out_fig_path: str, **kwargs)#
Write the figure to a common image file type, such as .png or .jpg. Applies
plt.savefig()to figure object.- Parameters:
out_fig_path (str) – Path to the file the figure will be written to.
kwargs (dict) – Additional key word arguments passed to plt.savefig()
- property input_tac_path#
Gets the input TAC file path.
- property tacs_dir#
Gets the TAC directory path.
- property reference_tac_path#
Gets the reference TAC file path.
- is_valid_tacs_dir(tacs_dir: str)#
Validates the TAC directory by checking for TAC files.
- Parameters:
tacs_dir (str) – Directory to validate.
- Returns:
bool – True if valid, otherwise False.
- static get_tacs_list_from_dir(tacs_dir: str) list[str]#
Retrieves a sorted list of TAC file paths from a directory.
- Parameters:
tacs_dir (str) – Directory from which to retrieve TAC files.
- Returns:
list[str] – Sorted list of TAC file paths.
- static get_tacs_objects_dict_from_files_list(tacs_files_list: list[str])#
Creates a dict of TAC objects from a list of file paths.
- Parameters:
tacs_files_list (list[str]) – List of TAC file paths.
- Returns:
dict – Dictionary of region name-TAC object pairs.
- static get_tacs_objects_dict_from_dir(tacs_dir: str) dict#
Creates a dict of TAC objects from a directory of TAC files.
- Parameters:
tacs_dir (str) – A directory of TAC files.
- Returns:
dict – Dictionary of region name-TAC object pairs.
- static get_tacs_objects_list_from_files_list(tacs_files_list: list[str])#
Creates a list of TAC objects from a list of file paths.
- Parameters:
tacs_files_list (list[str]) – List of TAC file paths.
- Returns:
list[TimeActivityCurve] – List of TAC objects.
- static get_tacs_vals_from_objs_list(tacs_objects_list: list[TimeActivityCurve])#
Extracts TAC values from a list of TAC objects.
- Parameters:
tacs_objects_list (list[TimeActivityCurve]) – List of TAC objects.
- Returns:
list – List of TAC values.
- get_tacs_vals_from_dir(tacs_dir: str)#
Retrieves TAC values from files in a specified directory.
- Parameters:
tacs_dir (str) – Directory containing TAC files.
- Returns:
list – List of TAC values.
- static capitalize_first_char_of_str(input_str: str)#
Capitalize only the first character of a string, leaving the remainder unchanged.
- Parameters:
input_str (str) – The string to capitalize the first character of.
- Returns:
output_str (str) – The string with only the first character capitalized.
- static infer_segmentation_label_from_tac_path(tac_path: str, tac_id: int)#
Infers a segmentation label from a TAC file path by analyzing the filename.
This method extracts a segment label from the filename of a TAC file. It checks the presence of a seg- marker in the filename, which is followed by the segment name. This segment name is then formatted with each part capitalized. If no segment label is found, a default unknown label is generated using the TAC’s ID.
- Parameters:
tac_path (str) – Path of the TAC file.
tac_id (int) – ID of the TAC.
- Returns:
str – Inferred segmentation label.
- infer_segmentation_labels_for_tacs()#
Infers segmentation labels for TACs.
- Returns:
list[str] – List of inferred segmentation labels.