Skip to content

Commit 732a372

Browse files
committed
plugins and d15 script for extracting and plotting nps subregions from second level DAT object
1 parent c46bef5 commit 732a372

File tree

3 files changed

+263
-0
lines changed

3 files changed

+263
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% Extract and Plot all NPS sub regions
2+
% Once the CANlab second level batch prep_* sequence has been run, this
3+
% script will identify all NPS subregions in the DAT object and then
4+
% extract and plot those subregions one by one. Figures will automatically
5+
% be saved in the figsavedir, which is set in a_set_up_paths_always_run_first.m
6+
7+
% get list of all NPS sub regions
8+
regions = cat(2, DAT.NPSsubregions.posnames, DAT.NPSsubregions.negnames)
9+
10+
% Loop through DAT object and for each NPS sub region, extract data and
11+
% plot
12+
for i=1:length(regions)
13+
nps_local_object = plugin_extract_local_nps_region_vals(DAT, regions{i});
14+
plugin_plot_local_nps_region_vals(nps_local_object, figsavedir);
15+
end
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
function NPS_region_vals = plugin_extract_local_nps_region_vals(second_level_DAT_object, my_region)
2+
%% Helper function to extract local NPS region values from DAT object
3+
%
4+
%
5+
% This program is free software: you can redistribute it and/or modify
6+
% it under the terms of the GNU General Public License as published by
7+
% the Free Software Foundation, either version 3 of the License, or
8+
% (at your option) any later version.
9+
%
10+
% This program is distributed in the hope that it will be useful,
11+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
% GNU General Public License for more details.
14+
%
15+
% You should have received a copy of the GNU General Public License
16+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
% ..
18+
%
19+
% :Usage:
20+
% ::
21+
% **NOTE**: This script and it's counterpart,
22+
% plugin_plot_local_nps_region_vals.m, ONLY pertain to the CANlab 2nd level
23+
% script system. They are not for general use. The input data object
24+
% for this script is a DAT object, which is specific to the 2nd level
25+
% script system, and other data objects may not work as intended with
26+
% this function.
27+
%
28+
%
29+
% NPS_region_vals = extract_local_nps_region_vals(second_level_DAT_object, nps_region_of_interest)
30+
%
31+
%
32+
% :Inputs:
33+
% 1.) An CANlab 2nd level batch script DAT object: This is most likely created by using the
34+
% canlab 2nd level scripts, which can create a DAT object with the
35+
% property NPSsubregions. This function will use this property to extract
36+
% relevant data.
37+
%
38+
% 2.) An NPS region of interest: This can be any of the positive or
39+
% negative regions of the NPS, which include:
40+
%
41+
% posnames: {'vermis' 'rIns' 'rV1' 'rThal' 'lIns' 'rdpInsDATA_STRUCT' 'rS2_Op' 'dACC'}
42+
% negnames: {'rLOC' 'lLOC' 'rpLOC' 'pgACC' 'lSTS' 'rIPL' 'PCC'}
43+
%
44+
% :Outputs:
45+
% 1.) NPS_region_vals: A data structure with the following properties
46+
% A.) Region: This is the NPS region from which the function is extracting data
47+
% B.) condition_data: Values from input fmri_data_object
48+
% (usually DAT from 2nd level analysis) that correspond to NPS activation
49+
% in input my_region. Values grouped by conditions in input fmri_data_object.
50+
% This function pulls from fmri_data_object.NPSsubregions
51+
% C.) condition_names: Names of conditions from
52+
% fmri_data_object (usually DAT from 2nd level analysis) that
53+
% describe the condition_data groups. This function pulls from input
54+
% fmri_data_object.conditions
55+
% D.) contrast_data: Values from input fmri_data_object
56+
% (usually DAT from 2nd level analysis) that correspond to NPS activation
57+
% in input my_region. Values grouped by contrasts in input fmri_data_object.
58+
% This function pulls from fmri_data_object.NPSsubregions
59+
% E.) contrast_names: Names of contrasts from fmri_data_object
60+
% (usually DAT from 2nd level analysis) that describe the
61+
% contrast_data groups. This function pulls from input
62+
% fmri_data_object.contrastnames
63+
% F.) colors: Colors pulled from input fmri_data_object for plotting
64+
% by conditions
65+
% G.) contrast_colors: Colors pulled from input fmri_data_object for
66+
% plotting by contrasts
67+
% :Examples:
68+
% (1) NPS_region_vals = extract_local_nps_region_vals(DAT, 'dACC') %saves dACC
69+
% values to new variable NPS_region_vals
70+
%
71+
% (2)To load in a CANlab second-level analysis session, extract an NPS subregion, and
72+
% then plot it by condition/contrast:
73+
%
74+
% %reload second level analysis objects
75+
% addpath(genpath('/path/to/data/files'));
76+
% a_set_up_paths_always_run_first;
77+
% b_reload_saved_matfiles
78+
%
79+
% %Example to extract dACC from DAT
80+
% NPS_local_dACC = extract_local_nps_region_vals(DAT, 'dACC')
81+
%
82+
% %Example to plot newly extrated dACC object
83+
% plot_local_nps_region_vals(NPS_local_dACC)
84+
85+
86+
87+
88+
% Get INPUTS
89+
region = my_region;
90+
in_DAT = second_level_DAT_object;
91+
92+
% check input region and designate pos or neg NPS
93+
if sum(strcmp(in_DAT.NPSsubregions.posnames, region)) == 1
94+
pos_or_neg_field = 'npspos_by_region'; % set flag field to positive
95+
wh_region = strcmp(in_DAT.NPSsubregions.posnames, region); % find index of input region
96+
sprintf('Input region %s is an NPS Pos region.', region)
97+
98+
elseif sum(strcmp(in_DAT.NPSsubregions.negnames, region)) == 1
99+
pos_or_neg_field = 'npsneg_by_region'; % set flag field to negative
100+
wh_region = strcmp(in_DAT.NPSsubregions.negnames, region); % find index of input region
101+
sprintf('Input region %s is an NPS Neg region.', region)
102+
103+
else
104+
error('Bad Region Name')
105+
end
106+
107+
% Check input data for specified region for each condition
108+
region_index = find(wh_region);
109+
for i = 1:length(in_DAT.NPSsubregions.(pos_or_neg_field))
110+
if size(in_DAT.NPSsubregions.npspos_by_region{i}, 2) < region_index
111+
warning('Input object does not have NPS Data for specified region for every condition')
112+
end
113+
114+
115+
NPS_region_vals = [];
116+
NPS_region_vals.region = region;
117+
118+
k = length(in_DAT.conditions);
119+
kc = length(in_DAT.contrastnames);
120+
121+
% check for problems
122+
if sum(wh_region) > 1 || sum(wh_region) == 0, error('Bad region name'); end
123+
124+
% get the condition data
125+
NPS_region_vals.condition_data = cell(1, k);
126+
NPS_region_vals.conditions = in_DAT.conditions;
127+
128+
for i = 1:k
129+
NPS_region_vals.condition_data{i} = in_DAT.NPSsubregions.(pos_or_neg_field){i}(:, wh_region);
130+
end
131+
132+
% get the contrast data
133+
NPS_region_vals.contrastnames = in_DAT.contrastnames;
134+
135+
myfield = [pos_or_neg_field '_contrasts'];
136+
137+
for i = 1:kc
138+
NPS_region_vals.contrast_data{i} = in_DAT.NPSsubregions.(myfield){i}(:, wh_region);
139+
end
140+
141+
% Copy colors for use later in plotting
142+
NPS_region_vals.colors = in_DAT.colors;
143+
NPS_region_vals.contrastcolors = in_DAT.contrastcolors;
144+
145+
end
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
function plugin_plot_local_nps_region_vals(nps_region_extract, save_dir)
2+
%% Helper function to visualize local NPS region values from nps region extract (can be created by extract_local_nps_region_vals.m).
3+
%
4+
%
5+
% This program is free software: you can redistribute it and/or modify
6+
% it under the terms of the GNU General Public License as published by
7+
% the Free Software Foundation, either version 3 of the License, or
8+
% (at your option) any later version.
9+
%
10+
% This program is distributed in the hope that it will be useful,
11+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
% GNU General Public License for more details.
14+
%
15+
% You should have received a copy of the GNU General Public License
16+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
%
18+
%
19+
% :Usage:
20+
%
21+
% **NOTE**: This script and it's counterpart,
22+
% plugin_extract_local_nps_region_vals.m, ONLY pertain to the CANlab 2nd level
23+
% script system. They are not for general use. The input data object
24+
% for this script is a DAT object, which is specific to the 2nd level
25+
% script system, and other data objects may not work as intended with
26+
% this function.
27+
%
28+
% plot_local_nps_region_vals(NPS_region_values_object)
29+
%
30+
%
31+
% :Inputs:
32+
% 1.) An NPS_region_vals data object: This should be a data object including only
33+
% data from one NPS subregion. This object can be created using the
34+
% extract_local_nps_region_vals.m on a CANlab second level scripts DAT
35+
% object.
36+
%
37+
% :Outputs:
38+
% 1.) Two plots: One of the data from the NPS subregion of interest by
39+
% condition and one by contrast.
40+
%
41+
% :Examples:
42+
% (1) plot_local_nps_region_vals(NPS_region_vals)
43+
%
44+
% (2) To load in a CANlab second-level analysis session, extract an NPS subregion, and
45+
% then plot it by condition/contrast:
46+
%
47+
% %reload second level analysis objects
48+
% addpath(genpath('/path/to/data/files'));
49+
% a_set_up_paths_always_run_first;
50+
% b_reload_saved_matfiles
51+
%
52+
% %Example to extract dACC from DAT
53+
% NPS_local_dACC = extract_local_nps_region_vals(DAT, 'dACC')
54+
%
55+
% %Example to plot newly extrated dACC object
56+
% plot_local_nps_region_vals(NPS_local_dACC)
57+
58+
% Get input data
59+
DATA_STRUCT = nps_region_extract;
60+
num_cond = length(DATA_STRUCT.conditions); % get number of conditions
61+
num_cont = length(DATA_STRUCT.contrastnames); % get number of contrasts
62+
63+
empty_conditions = cell(1,num_cond); % set condition names to empty string to plot clear x-axis
64+
empty_contrasts = cell(1,num_cont); % set contrast names to empty string to plot clear x-axis
65+
66+
67+
% Create empty plot
68+
f1 = create_figure(DATA_STRUCT.region, 1, 2);
69+
70+
% Get condition data and specify options to be plotted
71+
mydata = DATA_STRUCT.condition_data;
72+
input_options = {'colors', DATA_STRUCT.colors, 'nofig', 'names', empty_conditions, 'noviolin', 'noind'};
73+
74+
% Plot condition data
75+
barplot_columns(mydata, input_options{:});
76+
77+
% Populate labels for conditions plot
78+
%title('Conditions');
79+
%ylabel(sprintf('%s local pattern response', DATA_STRUCT.region));
80+
xlabel(sprintf(''));
81+
ylabel(sprintf(''));
82+
83+
subplot(1, 2, 2);
84+
85+
% Get contrast data and specify options for plotting
86+
mydata = DATA_STRUCT.contrast_data;
87+
input_options = {'colors', DATA_STRUCT.contrastcolors, 'nofig', 'names', empty_contrasts, 'noviolin', 'noind'};
88+
89+
% Plot contrast data
90+
barplot_columns(mydata, input_options{:});
91+
92+
% Populate labels for contrasts plot
93+
%title('Contrasts');
94+
%ylabel(sprintf('%s local pattern response', DATA_STRUCT.region));
95+
xlabel(sprintf(''));
96+
ylabel(sprintf(''));
97+
98+
%save output as Support vector graphics file image (SVG)
99+
print(f1, [save_dir '/' DATA_STRUCT.region '_condition_contrast_plot'] , '-dsvg');
100+
close all;
101+
clear f1;
102+
sprintf('Image %s _condition_contrast_plot.svg was saved to: \n %s', DATA_STRUCT.region, save_dir)
103+
end

0 commit comments

Comments
 (0)