Home > data-vis > group_csv_data.m

group_csv_data

PURPOSE ^

cdata = group_csv_data(csv, grouping, IDs);

SYNOPSIS ^

function cdata = group_csv_data(csv, grouping, IDs)

DESCRIPTION ^

 cdata = group_csv_data(csv, grouping, IDs);

 Required: csv, grouping, IDs

 This function gets data from a single csv file, but groups data based on
 the categorical values indicated in the grouping file or matrix

 Notes:

 csv and grouping are csv filenames
 csv and grouping filenames should be absolute path if not located in
 /ein/multiwork/data_vis/correlation/

 Alternatively, grouping can be an input matrix of size Nx1 or Nx2 -- if
 Nx2, then first column should be subject IDs

 IDs can be a subject list or a list of experiments
 If grouping is a Nx1 matrix, then length(grouping) == length(cIDs(IDs))
 must be true

 Examples:
 ======
 csv = 'cstream_inhand_right-hand_obj-all_parent_prop_vs_age.csv';
 grouping = [1 2 2 3];
 IDs = [4301 4302 4303 4304];
 ======
 csv = 'cstream_inhand_right-hand_obj-all_parent_prop_vs_age.csv';
 grouping = '/ein/multiwork/data_vis/grouping_example.csv';
 IDs = 43;

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function cdata = group_csv_data(csv, grouping, IDs)
0002 % cdata = group_csv_data(csv, grouping, IDs);
0003 %
0004 % Required: csv, grouping, IDs
0005 %
0006 % This function gets data from a single csv file, but groups data based on
0007 % the categorical values indicated in the grouping file or matrix
0008 %
0009 % Notes:
0010 %
0011 % csv and grouping are csv filenames
0012 % csv and grouping filenames should be absolute path if not located in
0013 % /ein/multiwork/data_vis/correlation/
0014 %
0015 % Alternatively, grouping can be an input matrix of size Nx1 or Nx2 -- if
0016 % Nx2, then first column should be subject IDs
0017 %
0018 % IDs can be a subject list or a list of experiments
0019 % If grouping is a Nx1 matrix, then length(grouping) == length(cIDs(IDs))
0020 % must be true
0021 %
0022 % Examples:
0023 % ======
0024 % csv = 'cstream_inhand_right-hand_obj-all_parent_prop_vs_age.csv';
0025 % grouping = [1 2 2 3];
0026 % IDs = [4301 4302 4303 4304];
0027 % ======
0028 % csv = 'cstream_inhand_right-hand_obj-all_parent_prop_vs_age.csv';
0029 % grouping = '/ein/multiwork/data_vis/grouping_example.csv';
0030 % IDs = 43;
0031 
0032 if ischar(grouping)
0033     gdata = get_csv_data(grouping, 2, IDs);
0034     subgroup = gdata.sub_list(gdata.has_variable);
0035     grouping = gdata.data(gdata.has_variable);
0036     sublog = gdata.has_variable;
0037 else
0038     if min(size(grouping)) == 1
0039         if size(grouping, 2) > 1
0040             grouping = grouping';
0041         end
0042     end
0043     if size(grouping, 2) > 1
0044         subgroup = grouping(:,1);
0045         grouping = grouping(:,2:end);
0046     else
0047         subgroup = cIDs(IDs);
0048         if length(subgroup) ~= length(grouping)
0049             error('Grouping and sub_list from IDs must have equal length');
0050         end
0051     end
0052 end
0053 
0054 cdata = get_csv_data(csv,[], IDs);
0055 data = cdata.data;
0056 subdata = cdata.sub_list;
0057 if exist('sublog', 'var')
0058     flog = sublog & cdata.has_variable;
0059 else
0060     flog = cdata.has_variable;
0061 end
0062 [subs, idx, log] = intersect_order(subgroup, subdata);
0063 grouping = grouping(idx,:);
0064 data = data(log,:);
0065 ugroup = unique(grouping);
0066 final = cell(numel(ugroup), 1);
0067 gsub_list = cell(numel(ugroup), 1);
0068 data_isnan = cell(numel(ugroup), 1);
0069 for u = 1:numel(final)
0070     final{u,1} = data(grouping == ugroup(u),:);
0071     gsub_list{u,1} = subs(grouping == ugroup(u),:);
0072     data_isnan{u,1} = isnan(data(grouping==ugroup(u),:));
0073 end
0074 data_cat = vertcat(final{:});
0075 gsub_list_cat = vertcat(gsub_list{:});
0076 
0077 cdata.data = final;
0078 cdata.data_isnan = data_isnan;
0079 cdata.data_cat = data_cat;
0080 cdata.gsub_list = gsub_list;
0081 cdata.sub_list = gsub_list_cat;
0082 cdata.has_variable = flog;
0083 cdata.grouping_unique = ugroup;
0084 cdata.grouping = sort(grouping);
0085 cdata.data_cat_isnan = isnan(data_cat);
0086 if exist('gdata', 'var')
0087     cdata.grouping_headers = gdata.data_headers;
0088 end
0089 cdata = orderfields(cdata);
0090 end

Generated on Tue 23-May-2017 20:00:55 by m2html © 2005