Skip to content

Commit d7bb273

Browse files
committed
Import the SCN Core Support.
1 parent 6dc5c84 commit d7bb273

File tree

672 files changed

+104542
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

672 files changed

+104542
-0
lines changed

@canlab_dataset/add_var.m

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
% Not complete yet. Please edit me
2+
%
3+
% Function for adding a variable to a dataset in a systematic way.
4+
% - Checks IDs of subjects to make sure data is added in the correct order.
5+
% - Values for missing data are coded as missing values, as specified in dat.Description.Missing_Values
6+
% - Handles Subject_Level or Event_Level data
7+
8+
varname = 'ValenceType';
9+
var_to_add = SETUP.data.X;
10+
11+
% get indices
12+
[subjn, ia, ib] = intersect(dat.Subj_Level.id, subjname2, 'stable');
13+
14+
wh_subjs = false(length(dat.Subj_Level.id));
15+
wh_subjs(ia) = true;
16+
17+
n = length(dat.Subj_Level.id);
18+
19+
20+
% set missing value
21+
missingval = dat.Description.Missing_Values;
22+
if ischar(missingval), missingval = str2num(missingval);
23+
24+
25+
nmissing = length(dat.Subj_Level.id) - length(ia);
26+
fprintf('Adding variable %s:\n\tSubjects with missing data: %d\n', varname, nmissing);
27+
28+
nextra = length(subjname2) - length(ia);
29+
fprintf('\tSubjects not in dataset (will not be added): %d\n', nextra);
30+
31+
is_subj_level = iscell(var_to_add);
32+
33+
% Subject level
34+
% simplest case
35+
% --------------------------------------------------------------------
36+
if is_subj_level
37+
38+
newvar = repmat(missingval, n, 1);
39+
40+
newvar(ia) = var_to_add(ib);
41+
42+
return
43+
44+
end
45+
46+
% Event level
47+
% --------------------------------------------------------------------
48+
49+
for i = 1:n
50+
51+
52+
53+
end
54+
55+
56+
str2num('NaN')
57+

@canlab_dataset/bars.m

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
function [dat, descrip, colors, h1, s1] = bars(obj, varnames, varargin)
2+
%
3+
%
4+
% Takes any optional inputs to barplot_colored.m
5+
%
6+
% Bar plot for canlab_dataset object
7+
%
8+
% Usage:
9+
% -------------------------------------------------------------------------
10+
% [dat, descrip, colors, h1, s1] = bars(obj, varnames, varargin)
11+
% Takes any optional inputs to barplot_colored.m
12+
%
13+
% Author and copyright information:
14+
% -------------------------------------------------------------------------
15+
% Copyright (C) 2013 Tor Wager
16+
%
17+
% This program is free software: you can redistribute it and/or modify
18+
% it under the terms of the GNU General Public License as published by
19+
% the Free Software Foundation, either version 3 of the License, or
20+
% (at your option) any later version.
21+
%
22+
% This program is distributed in the hope that it will be useful,
23+
% but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
% GNU General Public License for more details.
26+
%
27+
% You should have received a copy of the GNU General Public License
28+
% along with this program. If not, see <http://www.gnu.org/licenses/>.
29+
%
30+
% Inputs:
31+
% -------------------------------------------------------------------------
32+
% obj canlab_dataset object
33+
% varnames Cell string of variable names to plot
34+
%
35+
% Takes any optional inputs to barplot_colored.m
36+
%
37+
% Outputs:
38+
% -------------------------------------------------------------------------
39+
% dat data matrix for each variable
40+
%
41+
% Examples:
42+
% -------------------------------------------------------------------------
43+
%
44+
% create_figure('NPS values - All subjects');
45+
%
46+
% varnames = {'15' '13' '11' ' 9' '16' '14' '12' '10'};
47+
% xvals = [1 2 4 5 8 9 11 12];
48+
% colors = {[1 0 0] [0 1 0] [1 0 0] [0 1 0] [1 0 0] [0 1 0] [1 0 0] [0 1 0]};
49+
% bars(LevoNPS, varnames, 'x', xvals, 'colors', colors, 'XTickLabels', varnames, 'within', 'nofig');
50+
%
51+
%
52+
% See also:
53+
% * list other functions related to this one, and alternatives*
54+
55+
% Programmers' notes:
56+
% List dates and changes here, and author of changes
57+
58+
n = length(varnames);
59+
colors = scn_standard_colors(n);
60+
61+
62+
% -------------------------------------------------------------------------
63+
% DEFAULTS AND INPUTS
64+
% -------------------------------------------------------------------------
65+
66+
% optional inputs with default values
67+
% -----------------------------------
68+
% - allowable_args is a cell array of argument names
69+
% - avoid spaces, special characters, and names of existing functions
70+
% - variables will be assigned based on these names
71+
% i.e., if you use an arg named 'cl', a variable called cl will be
72+
% created in the workspace
73+
74+
allowable_args = {'colors' 'nofig'};
75+
76+
default_values = {colors [0]};
77+
78+
% define actions for each input
79+
% -----------------------------------
80+
% - cell array with one cell for each allowable argument
81+
% - these have special meanings in the code below
82+
% - allowable actions for inputs in the code below are: 'assign_next_input' or 'flag_on'
83+
84+
actions = {'assign_next_input' 'flag_on'};
85+
86+
% logical vector and indices of which inputs are text
87+
textargs = cellfun(@ischar, varargin);
88+
whtextargs = find(textargs);
89+
90+
for i = 1:length(allowable_args)
91+
92+
% assign default
93+
% -------------------------------------------------------------------------
94+
95+
eval([allowable_args{i} ' = default_values{i};']);
96+
97+
wh = strcmp(allowable_args{i}, varargin(textargs));
98+
99+
if any(wh)
100+
% Optional argument has been entered
101+
% -------------------------------------------------------------------------
102+
103+
wh = whtextargs(wh);
104+
if length(wh) > 1, warning(['input ' allowable_args{i} ' is duplicated.']); end
105+
106+
switch actions{i}
107+
case 'assign_next_input'
108+
eval([allowable_args{i} ' = varargin{wh(1) + 1};']);
109+
varargin{wh(1) + 1} = [];
110+
111+
case 'flag_on'
112+
eval([allowable_args{i} ' = 1;']);
113+
114+
otherwise
115+
error(['Coding bug: Illegal action for argument ' allowable_args{i}])
116+
end
117+
118+
end % argument is input
119+
end
120+
121+
% END DEFAULTS AND INPUTS
122+
% -------------------------------------------------------------------------
123+
124+
% REST OF BAR PLOT
125+
126+
127+
[dat, ~, ~, descrip] = get_var(obj, varnames, varargin{:});
128+
129+
descrip = cat(1, descrip{:});
130+
131+
132+
colors = cat(1, colors{:});
133+
134+
xvals = 1:n;
135+
136+
if nofig
137+
else
138+
139+
create_figure('bars');
140+
end
141+
142+
[h1, s1] = barplot_colored(dat, varargin{:});
143+
% set(h2, 'BarWidth', .9)
144+
colormap(colors)
145+
146+
fprintf('T-tests against zero\n');
147+
fprintf('Description\tt(%3.0f)\tp\n', size(dat, 1) - 1);
148+
149+
% Print table - t-test against zero
150+
for i = 1:n
151+
[h, p, ci, stats] = ttest(dat(:, i));
152+
if p < .001, str = '***';
153+
elseif p < .01, str = '**';
154+
elseif p < .05, str = '*';
155+
elseif p < .10, str = '+';
156+
else str = ' ';
157+
end
158+
159+
fprintf('%s\t%3.2f\t%3.6f\t%s\n', descrip{i}, stats.tstat, p, str);
160+
end
161+
162+
163+
end
164+

@canlab_dataset/canlab_dataset.m

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
% D = canlab_dataset();
2+
%
3+
% Constructs a new, empty instance of a canlab_dataset object.
4+
% The fields of this object can subsequently be assigned data.
5+
%
6+
% The dataset (D) The dataset (D) is a way of collecting behavioral data
7+
% and/or meta-data about fMRI (and other) studies in a standardized format.
8+
% It has entries for experiment description, Subject-level
9+
% variables, Event-level variables (e.g., for fMRI events or trials in a
10+
% behavioral experiment) and other fields for sub-event level data (e.g.,
11+
% physio or continuous ratings within trials).
12+
%
13+
% See D.Description for some basic info.
14+
%
15+
% Dataset methods include:
16+
%
17+
% write_text -> writes a flat text file (data across all subjects)
18+
% concatenate -> returns flat matrix of data across all subjects to the workspace
19+
% get_var -> get event-level data from one variable and return in matrix and cell formats
20+
% scatterplot -> standard or multi-line scatterplot of relationship between two variables
21+
% scattermatrix -> Scatterplot matrix of pairwise event-level variables
22+
%
23+
% - type methods(canlab_dataset) for a list of all methods
24+
%
25+
% Copyright Tor Wager, 2013
26+
27+
classdef canlab_dataset
28+
29+
properties
30+
31+
% Build a dataset containing the key variables
32+
33+
34+
Description = struct('Experiment_Name', [], 'Missing_Values', [], 'Subj_Level', [], 'Event_Level', []);
35+
36+
Subj_Level = struct('id', cell(1), 'names', cell(1), 'type', cell(1), 'units', cell(1), 'descrip', cell(1), 'data', []);
37+
38+
Event_Level = struct('names', cell(1), 'type', cell(1), 'units', cell(1), 'descrip', cell(1), 'data', cell(1));
39+
40+
Sub_Event_Level = struct('names', cell(1), 'type', cell(1), 'units', cell(1), 'descrip', cell(1), 'data', cell(1));
41+
42+
Continuous = struct('names', cell(1), 'type', cell(1), 'units', cell(1), 'descrip', cell(1), 'data', cell(1));
43+
44+
wh_keep = struct();
45+
46+
end % properties
47+
48+
%% Subject-level data
49+
50+
methods
51+
52+
% Class constructor
53+
function obj = canlab_dataset(varargin)
54+
55+
obj.Description.Subj_Level{1, 1} = 'id: Unique text identifier for each subject (e.g., subject code)';
56+
57+
obj.Description.Subj_Level{2, 1} = 'data: cell array, one cell per subject, with rectangular data matrix.';
58+
obj.Description.Subj_Level{3, 1} = ' Each row is one event, each column a variable, with names in .names';
59+
60+
obj.Description.Event_Level{1, 1} = 'data: cell array, one cell per subject, with rectangular data matrix.';
61+
obj.Description.Event_Level{2, 1} = ' Each row is one event, each column a variable, with names in .names';
62+
63+
64+
end % constructor function
65+
66+
end % methods
67+
68+
69+
end % classdef
70+
71+
72+

@canlab_dataset/concatenate.m

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function [names, ids, dat, descrips] = concatenate(D, varargin)
2+
%
3+
% Concatenates Subject-level and Event-level data across all subjects
4+
%
5+
% [names ids dat] = concatenate(D)
6+
%
7+
% INPUT:
8+
% D: canlab_dataset
9+
% varargin: currently accepts a wh_keep (logical array)
10+
%
11+
% OUTPUT:
12+
% Names: cell array of variable names
13+
%
14+
% Descrip: cell array of variable descriptions
15+
%
16+
% ids: subject IDs matching data rows in dat
17+
%
18+
% dat: subjects*events x variables matrix
19+
%
20+
% - subject number, event number are included
21+
% - all subject-level and event-level data are included
22+
% - this format appropriate for, e.g., SAS/HLM
23+
%
24+
% Examples:
25+
% [names, ids, flatdat] = concatenate(D);
26+
% id_numbers = flatdat(:, 1);
27+
%
28+
% wh_subjs = true(size(D.Subj_Level.id));
29+
% wh_subjs([13 18 19]) = false;
30+
% [names, ids, dat] = concatenate(D, wh_subjs);
31+
%
32+
% % Copyright Tor Wager, 2013
33+
34+
% select subjects
35+
wh_ids = true(length(D.Subj_Level.id), 1);
36+
if length(varargin) > 0, wh_ids = logical(varargin{1}); end
37+
whfind = find(wh_ids);
38+
39+
names = {'subj_num' D.Subj_Level.names{:} 'Event_number' D.Event_Level.names{:}};
40+
descrips = {'subj_num' D.Subj_Level.descrip{:} 'Event_number' D.Event_Level.descrip{:}};
41+
42+
43+
n = length(D.Subj_Level.id(wh_ids));
44+
45+
x = D.Event_Level.data(wh_ids);
46+
47+
48+
for i = 1:n
49+
50+
e = size(D.Event_Level.data{whfind(i)}, 1);
51+
52+
ids{i} = repmat(D.Subj_Level.id{whfind(i)}, e, 1);
53+
54+
% subj ID subj level data trialID event data
55+
x{i} = [repmat(i, e, 1) repmat(D.Subj_Level.data(whfind(i), :), e, 1) (1:e)' x{i}];
56+
57+
end
58+
59+
ids = strvcat(ids{:});
60+
61+
dat = cat(1, x{:});
62+
63+
end % function

0 commit comments

Comments
 (0)