% analysis_data_Gameplay
clear all; close all; clc
addpath( genpath( 'D:\cDATA\Util\matlab_codes' ) )
addpath( genpath( 'C:\DATA\Util\matlab_codes' ) )



%% Event name info.
%
% TRIGGER_ODDBALL_START          = 20;
% TRIGGER_ODDBALL_STANDARD_EVENT = 22;
% TRIGGER_ODDBALL_RARE_EVENT     = 25;
% TRIGGER_ODDBALL_DONE           = 29;
%
% TRIGGER_GAME_SHOOT_BUTTON       = 90;
% TRIGGER_GAME_PLAYER_CRASH_WALL  = 91;
% TRIGGER_GAME_PLAYER_CRASH_ENEMY = 92;
% TRIGGER_GAME_COLLECT_STAR       = 93;
% TRIGGER_GAME_COLLECT_AMMO       = 94;
% TRIGGER_GAME_MISSILE_HIT_ENEMY  = 97;
% TRIGGER_GAME_START              = 99;
% TRIGGER_GAME_OVER               = 100;
%

%% FFT
% %
% % flist = dir( 'raw_*_2.mat' );
% %
% %
% % fft_data = [];
% %
% % for fn = 1 : size( flist, 1 )
% %
% %     fname = flist( fn ).name;
% %
% %     load( fname )
% %
% %     fs = raw.srate;
% %     data = raw.data;
% %
% %
% %     data = double( data );
% %
% %     data_length = size( data, 2 );
% %     time_length = fs / 0.001;
% %     fft_freq = [ 0 : 1 : time_length ] / ( time_length / fs );
% %     fft_freq = fft_freq( 2 : 50001 );% 0.001 - 50 Hz
% %
% %     for ch  = 1 : 63
% %         t_fft_data = [];
% %         for iter = 1 : 100
% %             idx_itv = randperm( data_length - time_length, 1 ) + [ 1 : time_length ];
% %             t_fft_data( iter, : ) = abs( fft( data( ch, idx_itv ) ) );
% %         end; clear iter
% %         fft_data( ch, :, fn ) = mean( t_fft_data( :, 2 : 50001 ), 1 );% 0.001 - 50 Hz
% %         clear t_fft_data idx_itv
% %     end; clear ch
% %
% %     clear raw fs data data_length time_length
% %
% %     disp( [ num2str( fn ) ] )
% %
% % end; clear fn
% %
% %
% % save( 'results_FFT.mat', 'fft_freq', 'fft_data' )
% %
% %
% % load( 'results_FFT.mat' )
% % fft_data = single( fft_data );
% % save( 'results_FFT.mat', 'fft_freq', 'fft_data' )
% %

%% View
%
% flist = dir( 'proc_*_2.mat' );
%
% fn = 17;% : size( flist, 1 )
%
% fname = flist( fn ).name;
%
% load( fname )
%
%
%
% data_ph = [];
% for ch = 1 : size( data_IS,1 )
%     data_ph( ch, : ) = angle( hilbert( data_IS( ch, : ) ) );
% end; clear ch
%
%
%
% figure( 'position', [ 100, 100, 400, 300 ])
%
% subplot( 2, 1, 1 )
% sort_data = sort( data_IS( : ) );
% sort_data = sort_data( round( [ 0.1, 0.25, 0.5, 0.75, 0.9 ] * length( sort_data ) ) );
% imagesc( data_IS, max( abs( [ sort_data( 1 ), sort_data( 5 ) ] ) ) * [ -1, 1 ] )
% colormap( gca, jet )
% colorbar
% ylabel( 'Channel' )
% % xlabel( 'Time (s)' )
% title( '1. Bandpass filtered data (0.001 - 0.1 Hz)' )
% clear sort_data
%
% subplot( 2, 1, 2 )
% imagesc( data_ph, 'alphadata', ~isnan( data_ph ), pi * [ -1, 1 ] )
% colormap( gca, hsv )
% colorbar
% ylabel( 'Channel' )
% xlabel( 'Time (s)' )
% title( '2. Phase of data' )
%
%
%
% names_event = {};
% names_event{ 1, 1 } = 'SHOOT BUTTON';
% names_event{ 2, 1 } = 'COLLECT STAR';
% names_event{ 3, 1 } = 'COLLECT AMMO';
% names_event{ 4, 1 } = 'PLAYER CRASH WALL';
% names_event{ 5, 1 } = 'PLAYER CRASH ENEMY';
% names_event{ 6, 1 } = 'MISSILE HIT ENEMY';
%
%
% hist_ph = [];
% N_samples = [];
% for tEvent = 1 : 6
%
%     if tEvent == 1
%         idx = event( 1, event( 2, : ) == 90 );% SHOOT BUTTON
%     elseif tEvent == 2
%         idx = event( 1, event( 2, : ) == 93 );% COLLECT STAR
%     elseif tEvent == 3
%         idx = event( 1, event( 2, : ) == 94 );% COLLECT AMMO
%     elseif tEvent == 4
%         idx = event( 1, event( 2, : ) == 91 );% PLAYER CRASH WALL
%     elseif tEvent == 5
%         idx = event( 1, event( 2, : ) == 92 );% PLAYER CRASH ENEMY
%     elseif tEvent == 6
%         idx = event( 1, event( 2, : ) == 97 );% MISSILE HIT ENEMY
%     end
%     if ismember( 0, idx )
%         idx( idx == 0 ) = 1;
%     end
%     idx = idx( idx <= size( data_IS, 2 ) );
%
%     for ch = 1 : size( data_ph, 1 )
%         hist_ph( ch, :, tEvent ) = histcounts( data_ph( ch, idx ), linspace( -pi, pi, 9 ) );
%     end; clear ch
%
%     N_samples( tEvent, 1 ) = size( idx, 2 );
%
%     clear idx
%
% end; clear tEvent
%
% hist_ph0 = [];
% for ch = 1 : size( data_ph, 1 )
%     hist_ph0( ch, : ) = histcounts( data_ph( ch, : ), linspace( -pi, pi, 9 ) );
% end; clear ch
%
%
%
% figure( 'position', [ 100, 100, 700, 400 ] )
% for tEvent = 1 : 6
%     subplot( 2, 3, tEvent )
%     n_hist_ph = hist_ph( :, :, tEvent ) ./ hist_ph0;
%     n_hist_ph( isinf( n_hist_ph ) ) = NaN;
%     n_hist_ph = bsxfun( @rdivide, n_hist_ph, sum( n_hist_ph, 2, 'omitnan' ) );
%     imagesc( n_hist_ph, 'alphadata', ~isnan( n_hist_ph ), [ 0, 0.3 ] )
%     colorbar
%     set( gca, 'xtick', [ 0.5, 2.5, 4.5, 6.5, 8.5 ], 'xticklabel', { '-\pi', '-\pi/2', '0', '\pi/2', '\pi' } )
%     ylabel( 'Channel' )
%     xlabel( 'Phase (rad)' )
%     title( [ names_event{ tEvent, 1 }, ' (', num2str( N_samples( tEvent ) ), ')' ] )
%     clear n_hist_ph
% end; clear tEvent
%
%
%
% hist_freq = [];
% for freq = 1 : 5
%
%     if freq == 1
%         data = data_D;
%     elseif freq == 2
%         data = data_T;
%     elseif freq == 3
%         data = data_A;
%     elseif freq == 4
%         data = data_B1;
%     elseif freq == 5
%         data = data_B2;
%     end
%
%     edges = linspace( -pi, pi, 9 );
%     for ch = 1 : size( data_ph, 1 )
%         for ph = 1 : length( edges ) - 1
%             if ph == 1
%                 idx = data_ph( ch, : ) >= edges( ph ) & data_ph( ch, : ) <= edges( ph + 1 );
%             else
%                 idx = data_ph( ch, : ) > edges( ph ) & data_ph( ch, : ) <= edges( ph + 1 );
%             end
%             hist_freq( ch, ph, freq ) = sum( data( ch, idx ), 2 );
%             clear idx
%         end; clear ph
%     end; clear ch
%
%     clear data edges
%
% end; clear freq
%
%
%
% names_freq = {};
% names_freq{ 1, 1 } = '1 - 3 HZ';
% names_freq{ 2, 1 } = '5 - 7 Hz';
% names_freq{ 3, 1 } = '9 - 12 Hz';
% names_freq{ 4, 1 } = '14 - 20 Hz';
% names_freq{ 5, 1 } = '22 - 30 Hz';
%
%
%
% figure( 'position', [ 100, 100, 700, 400 ] )
% for freq = 1 : 5
%     subplot( 2, 3, freq )
%     n_hist_freq = hist_freq( :, :, freq ) ./ hist_ph0;
%     n_hist_freq( isinf( n_hist_freq ) ) = NaN;
%     n_hist_freq = bsxfun( @rdivide, n_hist_freq, sum( n_hist_freq, 2, 'omitnan' ) );
%     imagesc( n_hist_freq, 'alphadata', ~isnan( n_hist_freq ), [ 0, 0.2 ] )
%     colorbar
%     set( gca, 'xtick', [ 0.5, 2.5, 4.5, 6.5, 8.5 ], 'xticklabel', { '-\pi', '-\pi/2', '0', '\pi/2', '\pi' } )
%     ylabel( 'Channel' )
%     xlabel( 'Phase (rad)' )
%     title( [ names_freq{ freq, 1 } ] )
%     clear n_hist_freq
% end; clear freq
%

%% Phase histogram
% %
% % ph_edges = linspace( -pi, pi, 9 );
% % ph_mean = ( ph_edges( 1 : end - 1 ) + ph_edges( 2 : end ) ) / 2;
% %
% % flist = dir( 'proc_*_2.mat' );
% %
% %
% % hist_ph = [];
% % N_samples = [];
% % L_samples = [];
% % hist_ph0 = [];
% % for fn = 1 : size( flist, 1 )
% %
% %     fname = flist( fn ).name;
% %
% %     load( fname )
% %
% %     data_ph = [];
% %     for ch = 1 : size( data_IS,1 )
% %         data_ph( ch, : ) = angle( hilbert( data_IS( ch, : ) ) );
% %     end; clear ch
% %
% %
% %     for tEvent = 1 : 6
% %
% %         if tEvent == 1
% %             idx = event( 1, event( 2, : ) == 90 );% SHOOT BUTTON
% %         elseif tEvent == 2
% %             idx = event( 1, event( 2, : ) == 93 );% COLLECT STAR
% %         elseif tEvent == 3
% %             idx = event( 1, event( 2, : ) == 94 );% COLLECT AMMO
% %         elseif tEvent == 4
% %             idx = event( 1, event( 2, : ) == 91 );% PLAYER CRASH WALL
% %         elseif tEvent == 5
% %             idx = event( 1, event( 2, : ) == 92 );% PLAYER CRASH ENEMY
% %         elseif tEvent == 6
% %             idx = event( 1, event( 2, : ) == 97 );% MISSILE HIT ENEMY
% %         end
% %         if ismember( 0, idx )
% %             idx( idx == 0 ) = 1;
% %         end
% %         idx = idx( idx <= size( data_IS, 2 ) );
% %
% %         for ch = 1 : size( data_ph, 1 )
% %             hist_ph( ch, :, tEvent, fn ) = histcounts( data_ph( ch, idx ), ph_edges );
% %         end; clear ch
% %
% %         N_samples( tEvent, fn ) = size( idx, 2 );
% %
% %         clear idx
% %
% %     end; clear tEvent
% %
% %
% %     for ch = 1 : size( data_ph, 1 )
% %         hist_ph0( ch, :, fn ) = histcounts( data_ph( ch, : ), ph_edges );
% %     end; clear ch
% %
% %
% %     L_samples( 1, fn ) = size( data_IS, 2 );
% %
% %
% %     clear data_ph
% %     clear fs fss data_IS data_D data_T data_A data_B1 data_B2 event
% %     clear fname
% %
% % end; clear fn
% %
% %
% % % =========================================================================
% %
% %
% % names_event = {};
% % names_event{ 1, 1 } = 'Missile launch button';
% % names_event{ 2, 1 } = 'Collect star';
% % names_event{ 3, 1 } = 'Collect ammo box';
% % names_event{ 4, 1 } = 'Crash into wall';
% % names_event{ 5, 1 } = 'Crash into enemy';
% % names_event{ 6, 1 } = 'Missile hit enemy';
% %
% %
% % % =========================================================================
% %
% %
% % n_hist = [];
% % MaxMin_loc = [];
% % MaxMin_val = [];
% % for tEvent = 1 : size( hist_ph, 3 )
% %     for fn = 1 : size( hist_ph, 4 )
% %
% %         n_hist_ph = hist_ph( :, :, tEvent, fn ) ./ hist_ph0( :, :, fn );
% %         n_hist_ph( isinf( n_hist_ph ) ) = NaN;
% %         n_hist_ph = bsxfun( @rdivide, n_hist_ph, sum( n_hist_ph, 2, 'omitnan' ) );
% %         n_hist_ph = 100 * bsxfun( @rdivide, n_hist_ph, 1 / size( n_hist_ph, 2 ) ) - 100;
% %         n_hist( :, :, tEvent, fn ) = n_hist_ph;
% %
% %         [ valMax, locMax ] = max( n_hist_ph, [], 2 );
% %         [ valMin, locMin ] = min( n_hist_ph, [], 2 );
% %         MaxMin_loc( :, [ 1, 2 ], tEvent, fn ) = [ locMax, locMin ];
% %         MaxMin_val( :, [ 1, 2 ], tEvent, fn ) = [ valMax, valMin ];
% %
% %         clear n_hist_ph valMax locMax valMin locMin
% %
% %     end; clear fn
% % end; clear tEvent
% %
% %
% % save( 'results_hist.mat', 'ch_info', 'ph_edges', 'ph_mean', 'hist_ph', 'hist_ph0', 'N_samples', 'L_samples', 'names_event', 'n_hist', 'MaxMin_loc', 'MaxMin_val' )
% %

%% Behavior
% %
% % flist = dir( 'proc_*_2.mat' );
% %
% %
% % seq_ratio1 = [];
% % seq_ratio2 = [];
% %
% % for fn = 1 : size( flist, 1 )
% %
% %     fname = flist( fn ).name;
% %
% %     load( fname )
% %
% %
% %     for e1 = 1 : 6
% %         for e2 = 1 : 6
% %
% %             tEvent = e1;
% %             if tEvent == 1
% %                 idx = event( 1, event( 2, : ) == 90 );% SHOOT BUTTON
% %             elseif tEvent == 2
% %                 idx = event( 1, event( 2, : ) == 93 );% COLLECT STAR
% %             elseif tEvent == 3
% %                 idx = event( 1, event( 2, : ) == 94 );% COLLECT AMMO
% %             elseif tEvent == 4
% %                 idx = event( 1, event( 2, : ) == 91 );% PLAYER CRASH WALL
% %             elseif tEvent == 5
% %                 idx = event( 1, event( 2, : ) == 92 );% PLAYER CRASH ENEMY
% %             elseif tEvent == 6
% %                 idx = event( 1, event( 2, : ) == 97 );% MISSILE HIT ENEMY
% %             end
% %             if ismember( 0, idx )
% %                 idx( idx == 0 ) = 1;
% %             end
% %             idx1 = idx;
% %
% %             tEvent = e2;
% %             if tEvent == 1
% %                 idx = event( 1, event( 2, : ) == 90 );% SHOOT BUTTON
% %             elseif tEvent == 2
% %                 idx = event( 1, event( 2, : ) == 93 );% COLLECT STAR
% %             elseif tEvent == 3
% %                 idx = event( 1, event( 2, : ) == 94 );% COLLECT AMMO
% %             elseif tEvent == 4
% %                 idx = event( 1, event( 2, : ) == 91 );% PLAYER CRASH WALL
% %             elseif tEvent == 5
% %                 idx = event( 1, event( 2, : ) == 92 );% PLAYER CRASH ENEMY
% %             elseif tEvent == 6
% %                 idx = event( 1, event( 2, : ) == 97 );% MISSILE HIT ENEMY
% %             end
% %             if ismember( 0, idx )
% %                 idx( idx == 0 ) = 1;
% %             end
% %             idx2 = idx;
% %
% %             t_diff = bsxfun( @minus, idx2, transpose( idx1 ) );
% %             t_diff( t_diff < 0 ) = NaN;
% %             t_diff = min( t_diff, [], 2, 'omitnan' );
% %
% %             for iter = 1 : 10000
% %                 idx0 = poissrnd( length( idx1 ) / size( data_IS, 2 ), 1, size( data_IS, 2 ) );
% %                 idx1_pseudo = [];
% %                 for k = 1 : max( idx0, [], 2 )
% %                     idx1_pseudo = [ idx1_pseudo, find( idx0 == k ) ];
% %                 end; clear k
% %                 idx1_pseudo = sort( idx1_pseudo );
% %                 t_diff_pseudo = bsxfun( @minus, idx2, transpose( idx1_pseudo ) );
% %                 t_diff_pseudo( t_diff_pseudo < 0 ) = NaN;
% %                 t_diff_pseudo = min( t_diff_pseudo, [], 2, 'omitnan' );
% %                 seq_ratio1( e1, e2, fn, iter ) = mean( t_diff, 1, 'omitnan' ) / mean( t_diff_pseudo, 1, 'omitnan' );
% %
% %                 idx0 = poissrnd( length( idx2 ) / size( data_IS, 2 ), 1, size( data_IS, 2 ) );
% %                 idx2_pseudo = [];
% %                 for k = 1 : max( idx0, [], 2 )
% %                     idx2_pseudo = [ idx2_pseudo, find( idx0 == k ) ];
% %                 end; clear k
% %                 idx2_pseudo = sort( idx2_pseudo );
% %                 t_diff_pseudo = bsxfun( @minus, idx2_pseudo, transpose( idx1 ) );
% %                 t_diff_pseudo( t_diff_pseudo < 0 ) = NaN;
% %                 t_diff_pseudo = min( t_diff_pseudo, [], 2, 'omitnan' );
% %                 seq_ratio2( e1, e2, fn, iter ) = mean( t_diff, 1, 'omitnan' ) / mean( t_diff_pseudo, 1, 'omitnan' );
% %
% %                 clear idx0 idx1_pseudo idx2_pseudo t_diff_pseudo
% %             end; clear iter
% %
% %             clear tEvent idx idx1 idx2 t_diff
% %         end; clear e2
% %     end; clear e1
% %
% %
% %     clear fs fss data_IS data_D data_T data_A data_B1 data_B2 event
% %     clear fname
% %
% %     disp( [ num2str( fn ) ] )
% %
% % end; clear fn
% %
% %
% % names_event = {};
% % names_event{ 1, 1 } = 'Missile launch button';
% % names_event{ 2, 1 } = 'Collect star';
% % names_event{ 3, 1 } = 'Collect ammo box';
% % names_event{ 4, 1 } = 'Crash into wall';
% % names_event{ 5, 1 } = 'Crash into enemy';
% % names_event{ 6, 1 } = 'Missile hit enemy';
% % 
% % 
% % save( 'results_behav.mat', 'seq_ratio1', 'seq_ratio2', 'names_event' )
% %

%%
% 
% load( 'results_FFT.mat' )
% 
% 
% ch = 24;% Cz
% % ch = 2;% Fz
% % ch = 50;% PO4
% % ch = 11;% CP5
% % ch = 5;% FT9
% figure( 'position', [ 100, 100, 400, 450 ] )
% for sub = 1 : 17
%     loglog( fft_freq, fft_data( ch, :, sub ) )
%     hold on
% end; clear sub
% loglog( fft_freq, median( fft_data( ch, :, : ), 3 ), '-k', 'linewidth', 3 )
% set( gca, 'xlim', [ 0.001, 10 ], 'xtick', [ 0.001, 0.01, 0.1, 1, 10 ] )
% ylabel( 'Magnitude' )
% xlabel( 'Frequency (Hz)' )
% 

%%
% 
% load( 'results_hist.mat' )
% 
% stat_N_samples = NaN( 6, 3 );
% for tEvent = 1 : 6
%     tstat = sort( N_samples( tEvent, : ) );
%     tstat = tstat( round( linspace( 1, length( tstat ), 5 ) ) );
%     stat_N_samples( tEvent, 1 : 3 ) = tstat( 2 : 4 );
%     clear tstat
% end; clear tEvent
% 
% 
% hw = 0.2;
% lw = 1;
% figure( 'position', [ 100, 100, 400, 450 ] )
% colors = colormap( turbo( 6 ) );
% hold on
% for tEvent = 1 : 6
%     plot( [ -1, 0 ], [ -1, -1 ], '-', 'color', colors( tEvent, : ), 'linewidth', lw )
% end; clear tEvent
% for tEvent = 1 : 6
%     for s = 1 : 3
%         plot( tEvent + hw * [ -1, 1 ], stat_N_samples( tEvent, s ) * [ 1, 1 ], '-', 'color', colors( tEvent, : ), 'linewidth', lw )
%     end; clear s
%     plot( tEvent + hw * [ -1, -1 ], [ stat_N_samples( tEvent, 1 ), stat_N_samples( tEvent, 3 ) ], '-', 'color', colors( tEvent, : ), 'linewidth', lw )
%     plot( tEvent + hw * [ 1, 1 ], [ stat_N_samples( tEvent, 1 ), stat_N_samples( tEvent, 3 ) ], '-', 'color', colors( tEvent, : ), 'linewidth', lw )
% end; clear tEvent
% set( gca, 'xlim', [ 0.5, 6.5 ], 'ylim', [ 0, 1000 ] )
% set( gca, 'xtick', [ 1 : 6 ], 'xticklabel', names_event )
% ylabel( 'Number of behavior occurrence' )
% xlabel( 'Behavior' )
% legend( 'Missile launch button', 'Collect star', 'Collect ammo box', 'Crash into wall', 'Crash into enemy', 'Missile hit enemy', 'location', 'northeast' )
% 

%%
% 
% load( 'results_behav.mat' )
% 
% 
% stat_seq_ratio1 = NaN( 6, 6, 3 );
% for e1 = 1 : 6
%     for e2 = 1 : 6
%         if e1 ~= e2
%             tstat = seq_ratio1( e1, e2, :, : );
%             tstat = sort( tstat( : ) );
%             tstat = tstat( round( linspace( 1, length( tstat ), 5 ) ) );
%             stat_seq_ratio1( e1, e2, 1 : 3 ) = tstat( 2 : 4 );
%             clear tstat
%         end
%     end; clear e2
% end; clear e1
% 
% stat_seq_ratio2 = NaN( 6, 6, 3 );
% for e1 = 1 : 6
%     for e2 = 1 : 6
%         if e1 ~= e2
%             tstat = seq_ratio2( e1, e2, :, : );
%             tstat = sort( tstat( : ) );
%             tstat = tstat( round( linspace( 1, length( tstat ), 5 ) ) );
%             stat_seq_ratio2( e1, e2, 1 : 3 ) = tstat( 2 : 4 );
%             clear tstat
%         end
%     end; clear e2
% end; clear e1
% 
% stat_seq_ratio1 = 100 * stat_seq_ratio1;
% stat_seq_ratio2 = 100 * stat_seq_ratio2;
% 
% 
% hw = 0.04;
% sw = 0.12;
% lw = 1;
% figure( 'position', [ 100, 100, 800, 450 ] )
% colors = colormap( turbo( 6 ) );
% 
% subplot( 1, 2, 1 )
% hold on
% for tEvent = 1 : 6
%     plot( [ -1, 0 ], [ -200, -200 ], '-', 'color', colors( tEvent, : ), 'linewidth', lw )
% end; clear tEvent
% plot( [ 0.5, 6.5 ], [ 100, 100 ], '-', 'color', 0.7 * [ 1, 1, 1 ] )
% for tEvent = 1 : 6
%     ct = 0;
%     for ttEvent = 1 : 6
%         if tEvent ~= ttEvent
%             ct = ct + 1;
%             for s = 1 : 3
%                 plot( tEvent + sw * ( ct - 3 ) + hw * [ -1, 1 ], stat_seq_ratio1( ttEvent, tEvent, s ) * [ 1, 1 ], '-', 'color', colors( ttEvent, : ), 'linewidth', lw )
%             end; clear s
%             plot( tEvent + sw * ( ct - 3 ) + hw * [ -1, -1 ], [ stat_seq_ratio1( ttEvent, tEvent, 1 ), stat_seq_ratio1( ttEvent, tEvent, 3 ) ], '-', 'color', colors( ttEvent, : ), 'linewidth', lw )
%             plot( tEvent + sw * ( ct - 3 ) + hw * [ 1, 1 ], [ stat_seq_ratio1( ttEvent, tEvent, 1 ), stat_seq_ratio1( ttEvent, tEvent, 3 ) ], '-', 'color', colors( ttEvent, : ), 'linewidth', lw )
%         end
%     end; clear ttEvent ct
% end; clear tEvent
% for k = 1 : 5
%     plot( k - 0.5 + [ 1, 1 ], [ -100, 500 ], ':k' )
% end; clear k
% set( gca, 'xlim', [ 0.5, 6.5 ], 'ylim', [ 0, 200 ] )
% set( gca, 'xtick', [ 1 : 6 ], 'xticklabel', names_event )
% ylabel( 'Timing relative to random (%)' )
% xlabel( 'Before' )
% legend( 'Missile launch button', 'Collect star', 'Collect ammo box', 'Crash into wall', 'Crash into enemy', 'Missile hit enemy', 'location', 'northwest' )
% 
% % subplot( 1, 2, 2 )
% % hold on
% % plot( [ 0.5, 6.5 ], [ 100, 100 ], '-', 'color', 0.7 * [ 1, 1, 1 ] )
% % for tEvent = 1 : 6
% %     ct = 0;
% %     for ttEvent = 1 : 6
% %         if tEvent ~= ttEvent
% %             ct = ct + 1;
% %             for s = 1 : 3
% %                 plot( tEvent + sw * ( ct - 3 ) + hw * [ -1, 1 ], stat_seq_ratio2( tEvent, ttEvent, s ) * [ 1, 1 ], '-', 'color', colors( ttEvent, : ), 'linewidth', lw )
% %             end; clear s
% %             plot( tEvent + sw * ( ct - 3 ) + hw * [ -1, -1 ], [ stat_seq_ratio2( tEvent, ttEvent, 1 ), stat_seq_ratio2( tEvent, ttEvent, 3 ) ], '-', 'color', colors( ttEvent, : ), 'linewidth', lw )
% %             plot( tEvent + sw * ( ct - 3 ) + hw * [ 1, 1 ], [ stat_seq_ratio2( tEvent, ttEvent, 1 ), stat_seq_ratio2( tEvent, ttEvent, 3 ) ], '-', 'color', colors( ttEvent, : ), 'linewidth', lw )
% %         end
% %     end; clear ttEvent ct
% % end; clear tEvent
% % for k = 1 : 5
% %     plot( k - 0.5 + [ 1, 1 ], [ -100, 500 ], ':k' )
% % end; clear k
% % set( gca, 'xlim', [ 0.5, 6.5 ], 'ylim', [ 0, 200 ] )
% % set( gca, 'xtick', [ 1 : 6 ], 'xticklabel', names_event )
% % xlabel( 'After' )
% 
% 
% F1 = [];% F-statistics (between var/within var)
% F2 = [];% F-statistics (between var/within var)
% for tEvent = 1 : 6
%     
%     stat = [];
%     ct = 0;
%     for ttEvent = 1 : 6
%         if tEvent ~= ttEvent
%             ct = ct + 1;
%             tstat = seq_ratio1( ttEvent, tEvent, :, : );
%             stat( :, ct ) = tstat( : );
%             clear tstat
%         end
%     end; clear ttEvent ct
%     
%     Fu = sum( size( stat, 1 ) * ( bsxfun( @minus, mean( stat, 1 ), mean( stat( : ), 1 ) ) .^ 2 ) / ( size( stat, 2 ) - 1 ), 2 );
%     Fd = sum( sum( bsxfun( @minus, stat, mean( stat, 1 ) ) .^ 2, 1 ), 2 ) / ( size( stat( : ), 1 ) - size( stat, 2 ) );
%     F1( 1, tEvent ) = Fu / Fd;
%     clear stat Fu Fd
%     
%     stat = [];
%     ct = 0;
%     for ttEvent = 1 : 6
%         if tEvent ~= ttEvent
%             ct = ct + 1;
%             tstat = seq_ratio2( tEvent, ttEvent, :, : );
%             stat( :, ct ) = tstat( : );
%             clear tstat
%         end
%     end; clear ttEvent ct
%     
%     Fu = sum( size( stat, 1 ) * ( bsxfun( @minus, mean( stat, 1 ), mean( stat( : ), 1 ) ) .^ 2 ) / ( size( stat, 2 ) - 1 ), 2 );
%     Fd = sum( sum( bsxfun( @minus, stat, mean( stat, 1 ) ) .^ 2, 1 ), 2 ) / ( size( stat( : ), 1 ) - size( stat, 2 ) );
%     F2( 1, tEvent ) = Fu / Fd;
%     clear stat Fu Fd
%     
% end; clear tEvent
% 
% 
% 
% % figure( 'position', [ 100, 100, 800 * 0.8, 450 * 0.8 ] )
% subplot( 1, 2, 2 )
% plot( [ 1 : 6 ], F1, 'om', 'markerfacecolor', 'm' )
% set( gca, 'xlim', [ 0.5, 6.5 ], 'ylim', [ 0, 1.2 * max( F1 ) ] )
% set( gca, 'xtick', [ 1 : 6 ], 'xticklabel', names_event )
% ylabel( 'F-statistics' )
% xlabel( 'Before' )
% % subplot( 1, 2, 2 )
% % plot( [ 1 : 6 ], F2, 'om', 'markerfacecolor', 'm' )
% % set( gca, 'xlim', [ 0.5, 6.5 ], 'ylim', [ 0, 1.2 * max( F2 ) ] )
% % set( gca, 'xtick', [ 1 : 6 ], 'xticklabel', names_event )
% % ylabel( 'F-statistics' )
% % xlabel( 'After' )
% 
% 
% 
% b_ratio1 = seq_ratio1( 1, 5, : , : );
% b_ratio1 = mean( b_ratio1, 4 );
% b_ratio1 = 100 * b_ratio1( : );
% [ ~, pVal ] = ttest( b_ratio1, 100, 'tail', 'left' )
% 
% b_ratio1 = seq_ratio1( 1, 6, : , : );
% b_ratio1 = mean( b_ratio1, 4 );
% b_ratio1 = 100 * b_ratio1( : );
% [ ~, pVal ] = ttest( b_ratio1, 100, 'tail', 'left' )
% 

%%
% 
% load( 'results_hist.mat' )
% 
% 
% load EEG_standard_elec1005
% ch_loc = [];
% for ch = 1 : 63
%     ch_name = ch_info( ch ).labels;
%     ch_idx = [];
%     for cht = 1 : length( elec_names )
%         ch_idx( cht, 1 ) = strcmpi( elec_names{ cht, 1 }, ch_name );
%     end; clear cht
%     ch_loc( ch, : ) = elec_loc( find( ch_idx ), : );
%     clear ch_name ch_idx
% end; clear ch
% clear elec_names elec_loc
% 
% 
% 
% % figure( 'position', [ 100, 100, 1000, 600 ] )
% % for tEvent = 1 : size( MaxMin_loc, 3 )
% %
% %     subplot( 2, 3, tEvent )
% %
% %     hist_MaxMin = zeros( length( ph_mean ), length( ph_mean ) );
% %     for fn = 1 : size( MaxMin_loc, 4 )
% %         for ch = 1 : size( MaxMin_loc, 1 )
% %             hist_MaxMin( MaxMin_loc( ch, 1, tEvent, fn ), MaxMin_loc( ch, 2, tEvent, fn ) ) = hist_MaxMin( MaxMin_loc( ch, 1, tEvent, fn ), MaxMin_loc( ch, 2, tEvent, fn ) ) + 1;
% %         end; clear ch
% %     end; clear fn
% %
% %     sort_data = sort( hist_MaxMin( : ) );
% %     sort_data = sort_data( round( linspace( 1, length( sort_data ), 10 ) ) );
% %
% %     imagesc( hist_MaxMin, [ 0, sort_data( end - 1 ) ] )
% %     colormap( gca, hot )
% %     set( gca, 'ytick', 0.5 + [ 0, length( ph_mean ) / 2, length( ph_mean ) ], 'yticklabel', { '-\pi', '0', '\pi' } )
% %     set( gca, 'xtick', 0.5 + [ 0, length( ph_mean ) / 2, length( ph_mean ) ], 'xticklabel', { '-\pi', '0', '\pi' } )
% %     ylabel( 'Phase at maximum occurrence (rad)' )
% %     xlabel( 'Phase at minimum occurrence (rad)' )
% %     title( [ names_event{ tEvent, 1 } ] )
% %
% %     clear hist_MaxMin sort_data
% % end; clear tEvent
% 
% 
% 
% ms = 5;
% figure( 'position', [ 100, 100, 800, 120 ] )
% colors_mag = colormap( parula );
% for tEvent = 1 : size( n_hist, 3 )
% 
%     mn_hist = mean( n_hist( :, :, tEvent, : ), 4 );
%     [ valMax, locMax ] = max( mn_hist, [], 2 );
%     [ valMin, locMin ] = min( mn_hist, [], 2 );
%     pVals = [];
%     for ch = 1 : size( mn_hist, 1 )
%         samples_max = squeeze( n_hist( ch, locMax( ch ), tEvent, : ) );
%         samples_min = squeeze( n_hist( ch, locMin( ch ), tEvent, : ) );
%         [ ~, pVals( ch, 1 ) ] = ttest2( samples_max, samples_min, 'tail', 'right' );
%         clear samples_max samples_min
%     end; clear ch
% 
%     subplot( 1, 6, tEvent )
%     hold on
%     color_idx = valMax - valMin;
%     color_idx = color_idx / 50;
%     color_idx( color_idx > 1 ) = 1;
%     for ch = 1 : size( mn_hist, 1 )
%         if pVals( ch ) < 0.05
%             plot( ch_loc( ch, 1 ), ch_loc( ch, 2 ), 'ok', 'markersize', ms, 'markerfacecolor', colors_mag( round( size( colors_mag, 1 ) * color_idx( ch ) ), : ) )
%         else
%             plot( ch_loc( ch, 1 ), ch_loc( ch, 2 ), 'ok', 'markersize', ms )
%         end
%     end; clear ch
%     axis off
%     axis image
%     % title( [ names_event{ tEvent, 1 } ] )
% 
%     clear mn_hist valMax locMax valMin locMin pVals color_idx
% end; clear tEvent
% 
% 
% 
% hw = 0.2;
% lw = 1;
% figure( 'position', [ 100, 100, 800, 200 ] )
% for tEvent = 1 : size( n_hist, 3 )
% 
%     mn_hist = mean( n_hist( :, :, tEvent, : ), 4 );
%     [ valMax, locMax ] = max( mn_hist, [], 2 );
%     [ valMin, locMin ] = min( mn_hist, [], 2 );
%     pVals = [];
%     for ch = 1 : size( mn_hist, 1 )
%         samples_max = squeeze( n_hist( ch, locMax( ch ), tEvent, : ) );
%         samples_min = squeeze( n_hist( ch, locMin( ch ), tEvent, : ) );
%         [ ~, pVals( ch, 1 ) ] = ttest2( samples_max, samples_min, 'tail', 'right' );
%         clear samples_max samples_min
%     end; clear ch
%     [ diffMax, ch ] = max( valMax - valMin, [], 1 );
%     disp( [ 'Event #', num2str( tEvent ), ' maximal difference = ', num2str( diffMax ), ' %' ] )
%     % disp( [ 'Event #', num2str( tEvent ), ' maximal difference at = ', num2str( ph_mean( locMax( ch ) ) ), ' rad' ] )
%     disp( [ 'Event #', num2str( tEvent ), ' maximal difference at = ', num2str( locMax( ch ) ), 'th phase step' ] )
%     %     stat = [];
%     %     for d = 1 : 8
%     %         tstat = sort( squeeze( n_hist( ch, d, tEvent, : ) ) );
%     %         tstat = tstat( round( linspace( 1, length( tstat ), 5 ) ) );
%     %         stat( d, 1 : 3 ) = tstat( 2 : 4 );
%     %         clear tstat
%     %     end; clear d
%     stat = [];
%     stat( 1, : ) = mean( n_hist( ch, :, tEvent, : ), 4 );
%     stat( 2, : ) = std( n_hist( ch, :, tEvent, : ), 0, 4 ) / sqrt( size( n_hist( ch, :, tEvent, : ), 4 ) );
% 
%     subplot( 1, 6, tEvent )
%     if pVals( ch, 1 ) < 0.05
%         hold on
%         %         for d = 1 : 8
%         %             for s = 1 : 3
%         %                 plot( ph_mean( d ) + hw * [ -1, 1 ], stat( d, s ) * [ 1, 1 ], '-', 'color', 'k', 'linewidth', lw )
%         %             end; clear s
%         %             plot( ph_mean( d ) + hw * [ -1, -1 ], [ stat( d, 1 ), stat( d, 3 ) ], '-', 'color', 'k', 'linewidth', lw )
%         %             plot( ph_mean( d ) + hw * [ 1, 1 ], [ stat( d, 1 ), stat( d, 3 ) ], '-', 'color', 'k', 'linewidth', lw )
%         %         end; clear d
%         patch( [ ph_mean, fliplr( ph_mean ) ], [ stat( 1, : ) - stat( 2, : ), fliplr( stat( 1, : ) + stat( 2, : ) ) ], 0.7 * [ 1, 1, 1 ], 'edgecolor', 0.7 * [ 1, 1, 1 ] )
%         plot( ph_mean, stat( 1, : ), '-', 'color', 'k', 'linewidth', lw )
%         plot( pi * [ -1, 1 ], [ 0, 0 ], ':k' )
%         set( gca, 'xlim', [ -pi, pi ], 'ylim', [ -50, 100 ] )
%         set( gca, 'xtick', [ -pi, 0, pi ], 'xticklabel', { '-\pi', '0', '\pi' } )
%         xlabel( 'ISA phase (rad)' )
%         if tEvent == 1
%             ylabel( 'Relative change to mean (%)' )
%         end
%         title( [ ch_info( ch ).labels ] )
%     end
% 
%     clear mn_hist valMax locMax valMin locMin pVals ch stat ampLim
% end; clear tEvent
% 
% 
% 
% ms = 10;
% figure( 'position', [ 100, 100, 800, 450 ] )
% colors_mag = colormap( copper );
% colors_ph = colormap( hsv );
% ct_tEvent = 0;
% for tEvent = [ 3, 4, 5 ]
%     ct_tEvent = ct_tEvent + 1;
% 
%     mn_hist = mean( n_hist( :, :, tEvent, : ), 4 );
%     [ valMax, locMax ] = max( mn_hist, [], 2 );
%     [ valMin, locMin ] = min( mn_hist, [], 2 );
%     pVals = [];
%     for ch = 1 : size( mn_hist, 1 )
%         samples_max = squeeze( n_hist( ch, locMax( ch ), tEvent, : ) );
%         samples_min = squeeze( n_hist( ch, locMin( ch ), tEvent, : ) );
%         [ ~, pVals( ch, 1 ) ] = ttest2( samples_max, samples_min, 'tail', 'right' );
%         clear samples_max samples_min
%     end; clear ch
% 
%     subplot( 2, 3, ct_tEvent )
%     hold on
%     color_idx = valMax - valMin;
%     % color_idx = color_idx / max( color_idx ) );
%     sort_idx = sort( color_idx( pVals < 0.05 ) );
%     sort_idx = sort_idx( round( linspace( 1, length( sort_idx ), 11 ) ) );
%     sort_idx = sort_idx( [ 2, 10 ] );
%     color_idx = color_idx - sort_idx( 1 );
%     color_idx = color_idx / ( sort_idx( 2 ) - sort_idx( 1 ) );
%     color_idx( color_idx < 1 / size( colors_mag, 1 ) ) = 1 / size( colors_mag, 1 );
%     color_idx( color_idx > 1 ) = 1;
%     for ch = 1 : size( mn_hist, 1 )
%         if pVals( ch ) < 0.05
%             plot( ch_loc( ch, 1 ), ch_loc( ch, 2 ), 'ok', 'markersize', ms, 'markerfacecolor', colors_mag( round( size( colors_mag, 1 ) * color_idx( ch ) ), : ) )
%         else
%             plot( ch_loc( ch, 1 ), ch_loc( ch, 2 ), 'ok', 'markersize', ms )
%         end
%     end; clear ch
%     axis off
%     axis image
%     % title( [ names_event{ tEvent, 1 } ] )
% 
%     subplot( 2, 3, 3 + ct_tEvent )
%     hold on
%     color_idx = ( ph_mean( locMax ) / (  2 * pi ) ) + 0.5;
%     for ch = 1 : size( mn_hist, 1 )
%         if pVals( ch ) < 0.05
%             plot( ch_loc( ch, 1 ), ch_loc( ch, 2 ), 'ok', 'markersize', ms, 'markerfacecolor', colors_ph( round( size( colors_ph, 1 ) * color_idx( ch ) ), : ) )
%         else
%             plot( ch_loc( ch, 1 ), ch_loc( ch, 2 ), 'ok', 'markersize', ms )
%         end
%     end; clear ch
%     axis off
%     axis image
% 
%     clear mn_hist valMax locMax valMin locMin pVals color_idx sort_idx
% end; clear tEvent ct_tEvent
% 
% 
% 
% ms = 7;
% figure( 'position', [ 100, 100, 800, 250 ] )
% ct_tEvent = 0;
% for tEvent = [ 3, 4, 5 ]
%     ct_tEvent = ct_tEvent + 1;
% 
%     mn_hist = mean( n_hist( :, :, tEvent, : ), 4 );
%     [ valMax, locMax ] = max( mn_hist, [], 2 );
%     [ valMin, locMin ] = min( mn_hist, [], 2 );
%     pVals = [];
%     for ch = 1 : size( mn_hist, 1 )
%         samples_max = squeeze( n_hist( ch, locMax( ch ), tEvent, : ) );
%         samples_min = squeeze( n_hist( ch, locMin( ch ), tEvent, : ) );
%         [ ~, pVals( ch, 1 ) ] = ttest2( samples_max, samples_min, 'tail', 'right' );
%         clear samples_max samples_min
%     end; clear ch
% 
%     vec_mag = valMax - valMin;
%     vec_mag = vec_mag( pVals < 0.05 );
%     vec_mag = 100 * ( vec_mag - min( vec_mag ) ) / ( max( vec_mag ) - min( vec_mag ) );
%     vec_phase = transpose( exp( 1i * ph_mean( locMax ) ) );
%     vec_phase = vec_phase( pVals < 0.05 );
%     vec_coord = bsxfun( @times, vec_mag, vec_phase );
%     vec_mean = mean( vec_coord, 1 );
%     b_line = transpose( linspace( -pi, pi, 1000 ) );
%     b_line = 100 * exp( 1i * b_line );
%     p_line = transpose( linspace( -pi, pi, 9 ) );
%     p_line = 100 * exp( 1i * p_line );
%     
%     subplot( 1, 3, ct_tEvent )
%     hold on
%     for p = 1 : size( b_line, 1 ) - 1
%         plot( [ real( b_line( p, 1 ) ), real( b_line( p + 1, 1 ) ) ], [ imag( b_line( p, 1 ) ), imag( b_line( p + 1, 1 ) ) ], '-k' )
%     end; clear p
%     for p = 1 : size( p_line, 1 ) - 1
%         plot( [ 0, real( p_line( p, 1 ) ) ], [ 0, imag( p_line( p, 1 ) ) ], ':k' )
%     end; clear p
%     for ch = 1 : size( vec_coord, 1 )
%         plot( real( vec_coord( ch, 1 ) ), imag( vec_coord( ch, 1 ) ), '+k', 'markersize', ms )
%     end; clear ch
%     plot( real( vec_mean ), imag( vec_mean ), 'or', 'markersize', ms, 'markerfacecolor', 'r' )
%     plot( [ 0, 100 * real( vec_mean ) / abs( vec_mean ) ], [ 0, 100 * imag( vec_mean ) / abs( vec_mean ) ], '-r' )
%     axis image
%     axis off
% 
%     clear mn_hist valMax locMax valMin locMin pVals vec_mag vec_phase vec_coord vec_mean b_line p_line
% end; clear tEvent ct_tEvent
% 
% 
% 
% figure( 'position', [ 100, 100, 200, 100 ] )
% imagesc( NaN, [ 0, 50 ] )
% colormap( parula )
% h = colorbar;
% h.Ticks = [ 0, 50 ];
% h.TickLabels = { '0 %', '50 %' };
% clear h
% 
% figure( 'position', [ 100, 100, 200, 100 ] )
% imagesc( NaN, [ 10, 90 ] )
% colormap( copper )
% h = colorbar;
% h.Ticks = [ 10, 90 ];
% h.TickLabels = { '10 %', '90 %' };
% clear h
% 
% figure( 'position', [ 100, 100, 200, 100 ] )
% imagesc( NaN, [ -pi, pi ] )
% colormap( hsv )
% h = colorbar;
% h.Ticks = [ -pi, -pi/2, 0, pi/2, pi ];
% h.TickLabels = { '-\pi', '-\pi/2', '0', '\pi/2', '\pi' };
% clear h
% 
