
%% Load data
datadir = '/Users/byeolkim/dropbox/FAST/sync/data/public sharing';
load(fullfile(datadir, 'figure1.mat'))

%% Figure 1c

close all;
cols = [0.9098    0.4902    0.4471
    [234 226 195]./255
    [209 234 224]./255
    [227 222 250]./255];
cols2 = cols+.2;
cols2(cols2>1) = 1;
cols3 = cols-.2;
n = 25;

% one run
for i = 3 
    
    x1 =interp1(1:n,rating_3d{i}(1:n,1),1:.01:n,'spline');
    y1 =interp1(1:n,rating_3d{i}(1:n,2),1:.01:n,'spline');
    z1 =interp1(1:n,rating_3d{i}(1:n,3),1:.01:n,'spline');
    
    plot3(x1,y1,z1, 'color', cols(1,:), 'linewidth', 3);
    hold on;
    
    scatter3(rating_3d{i}(2:n-1,1), rating_3d{i}(2:n-1,2), rating_3d{i}(2:n-1,3), 150, cols2(1,:), 'filled');
    scatter3(x1(1), y1(1), z1(1), 500, cols3(1,:), 'filled', 'p');
    scatter3(x1(end), y1(end), z1(end), 150, cols3(1,:), 'filled');
    
    text(rating_3d{i}(1:n,1)+.05, rating_3d{i}(1:n,2)+.05, rating_3d{i}(1:n,3), words_3d{i}(1:n), 'fontsize', 15, 'Rotation', 30);
end

% other 3 runs
k = 1;
for i = [1 2 4]
    
    x1 =interp1(1:n,rating_3d{i}(1:n,1),1:.01:n,'spline');
    y1 =interp1(1:n,rating_3d{i}(1:n,2),1:.01:n,'spline');
    z1 =interp1(1:n,rating_3d{i}(1:n,3),1:.01:n,'spline');
    k = k + 1;
    plot3(x1,y1,z1, 'color', cols(k,:), 'linewidth', 2);
    hold on;
    
end

set(gca, 'xlim', [0 1], 'ylim', [-1 1], 'zlim', [-1 1]);

xlabel('self-relevance');
ylabel('time');
zlabel('valence');

view(-27,39);
set(gcf, 'color', 'w', 'position', [937   802   528   492]);
set(gca, 'linewidth', 2, 'fontsize', 23, 'zlim', [-1 1], 'ylim', [-1 1], 'ztick', -1:1:1);

%% Figure 1d

close all;

cols = [213,62,79
    252,141,89
    254,224,139
    230,245,152
    153,213,148
    50,136,189]./255;

[u, idx_u] = unique(words);
u(ismember(u, {'na', ' na', '.'})) = [];

u_eng = words(:,idx_u);
u_eng(ismember(u, {'na', ' na', '.'})) = [];

A = zeros(numel(u), numel(u));

for i = [1:39 41:79 81:119 121:159]
    A(ismember(u, words(:,i)), ismember(u, words(:,i+1)))=A(ismember(u, words(:,i)), ismember(u, words(:,i+1)))+1;
end

valence = zeros(numel(u),1);
for i = 1:numel(u)
    valence(i,1) = mean(valence_all(ismember(words(:,:), u(i))));
end

bins = [-1 -1+2/6 -1+4/6 0 1-4/6 1-2/6 1];
g_idx = zeros(size(valence))';
for i = 1:6
    g_idx(valence >= bins(i) & valence < bins(i+1)) = i;
end
g_idx(valence == 1) = 6;

if any(g_idx==0)
    error('check g_idx');
end

AA = reformat_r_new(A, 'remove_diag');
h = vis_network(AA, 'manual_node_size', (sum(AA+AA',2)+.1)*30, 'group', g_idx', 'groupcolor', flipud(cols), 'line_pos_color', 'k', 'linewidth', .5, 'arrow', 'arrow_length', 7);

idx = sum(reformat_r_new(A, 'remove_diag'),2)>1;
text(h.node_xy(idx,1)+.05, h.node_xy(idx,2)+.1, u_eng(idx)', 'rotation', 10, 'fontsize', 9)

axes('Position', [-.08 .8 0.2 0.2]);
x = 0.1*ones(6,1);
y = [0 .5 1 1.5 2 2.5];
scatter(x, y, 40, flipud(cols), 'filled');
axis off;

text_legend = {'1>=Valence>=0.66', '0.66>Valence>=0.33', '0.33>Valence>=0', '0>=Valence>=-0.33',  '-0.33>=Valence>=-0.66', '-0.33>Valence>=-1'};
text_legend = fliplr(text_legend);
for i = 1:numel(x)
    text(x(i)+.1, y(i), text_legend{i});
end

set(gcf, 'Position', [142   791   746   391]);