

y1_r = [NaN						NaN						0.0235416666666667						0.015	];				
y2_r = [0.00865740740740741						0.0200462962962963						0.0152314814814815						0.0119444444444444	];				
y3_r = [0.0159722222222222						0.0306018518518519						0.0151851851851852						0.0124074074074074	];				
y4_r = [0.0102777777777778						0.0146759259259259						0.00893518518518518						0.00699074074074074	];				
y5_r = [0.00773148148148148						0.00738888888888889						0.011898148					0.007222222	];				
y6_r = [0.00953703703703704						0.0126851851851852						0.0125462962962963						0.0148611111111111];					
y7_r = [0.0134259259259259						0.0129166666666667						0.0105555555555556						0.00675925925925926];					
y8_r = [0.0075						0.0075462962962963						0.00939814814814815						0.00981481481481481	];				
y9_r = [0.0116203703703704						0.014537037037037						0.0125462962962963						0.00875		];			
y10_r = [0.0143981481481481						0.0131018518518519						0.0116203703703704						0.0124074074074074];					
y11_r = [0.01375						0.0125462962962963						0.012962962962963						0.0179166666666667		];			
y12_r = [0.0111574074074074						0.0166666666666667						0.0109259259259259						0.0148148148148148	];				
y13_r = [0.0177777777777778						0.017037037037037						0.00935185185185185						0.00699074074074074	];				
y14_r = [0.00689814814814815						0.00884259259259259						0.0101851851851852						0.00740740740740741	];				
y15_r = [0.00856481481481482						0.00967592592592593						0.0111111111111111						0.00337962962962963	];				
y16_r = [0.00671296296296296						0.00935185185185185						0.00949074074074074						0.00574074074074074];					
y17_r = [0.00842592592592593						0.01						0.0100462962962963						0.00601851851851852		];			
y18_r = [0.0062962962962963						0.00819444444444445						0.0111574074074074						0.00597222222222222	];				
y19_r = [0.00893518518518518						0.00962962962962963						0.010787037037037						0.00625				];	
y20_r = [0.00972222222222222						0.0107407407407407						0.0112037037037037						0.00537037037037037	];				
y21_r = [0.00865740740740741						0.0102314814814815						0.0105555555555556						0.00583333333333333	];				
y22_r = [0.0131481481481481						0.0125462962962963						0.00967592592592593						0.00564814814814815];					
y23_r = [0.00958333333333333						0.0128703703703704						0.0147222222222222						0.00837962962962963];					
y24_r = [0.0116203703703704						0.0147685185185185						0.0118981481481481						0.00796296296296296];					
y25_r = [0.01125						0.0116203703703704						0.0125925925925926						0.00643518518518519		];			
y26_r = [0.00814814814814815						0.0117592592592593						0.0145833333333333						0.00726851851851852	];				
y27_r = [0.00986111111111111						0.0135648148148148						0.0118981481481481						0.00680555555555556	];				
y28_r = [0.047037037037037						0.0112037037037037						0.0109722222222222						0.00935185185185185	];				
y29_r = [0.0123148148148148						0.0165277777777778						0.0141666666666667						0.00935185185185185	];				
y30_r = [0.0143518518518519						0.0195833333333333						0.0170833333333333						0.0228703703703704	];				
y31_r = [0.0193518518518519						0.0348148148148148						0.0322685185185185						0.0181018518518519	];				
y32_r = [0.0235648148148148						0.0281944444444444						0.0237962962962963						0.0128240740740741	];				
y33_r = [0.0177314814814815						0.0187962962962963						0.0193055555555556						NaN	];				
y34_r = [NaN						NaN						0.00416666666666667						NaN		];			
y35_r = [NaN						0.0219444444444444						0.04625						0.0476388888888889];					
y36_r = [0.0652777777777778						0.101111111111111						0.104861111111111						0.0965740740740741	];				
y37_r = [0.113009259259259						0.165						0.228842592592593						0.149861111111111			];		
y38_r = [0.199814814814815						0.267824074074074						0.295324074074074						0.211064814814815	];				
y39_r = [0.223518518518519						0.255694444444444						0.332962962962963						0.220740740740741	];				
y40_r = [0.244861111111111						0.306990740740741						0.343564814814815						0.214722222222222	];				
y41_r = [0.138796296296296						0.316296296296296						0.282361111111111						0.174166666666667	];				
y42_r = [0.168657407407407						0.248287037037037						0.267361111111111						0.161666666666667	];				
y43_r = [0.143194444444444						0.126990740740741						0.142824074074074						0.0469907407407407	];				
y44_r = [0.0749537037037037						0.0755092592592593						0.0637037037037037						0.024212962962963];					
y45_r = [0.0285185185185185						0.0415740740740741						0.0469907407407407						0.0183333333333333	];				
y46_r = [0.0235185185185185						0.0245833333333333						0.0359259259259259						0.0118981481481481	];				
y47_r = [0.0167592592592593						0.021712962962963						0.0208333333333333						0.0110185185185185	];				
y48_r = [0.0121296296296296						0.0175925925925926						0.0248611111111111						0.00865740740740741	];				
y49_r = [0.00981481481481481						0.014212962962963						0.0131018518518519						0.00824074074074074	];				
y50_r = [0.0116203703703704						0.015						0.0143518518518519						0.00930555555555555		];			
y51_r = [0.0134722222222222						0.0110648148148148						0.0118518518518519						0.00953703703703704	];				
y52_r = [0.0616203703703704						0.0670833333333333						0.0131018518518519						0.00902777777777778	];				
y53_r = [0.0158333333333333						0.0161574074074074						0.0141203703703704						0.00865740740740741	];				
y54_r = [0.0137962962962963						0.0144444444444444						0.0135185185185185						0.0075		];			
y55_r = [0.0118055555555556						0.0141203703703704						0.051712962962963						0.022962962962963	];				
y56_r = [0.00800925925925926						0.0087037037037037						0.0212962962962963						0.00726851851851852	];				
y57_r = [0.0115740740740741						0.0128240740740741						0.0108796296296296						0.0149537037037037	];				
y58_r = [0.0101388888888889						0.0140277777777778						0.0100462962962963						0.00861111111111111		];			
y59_r = [0.0116203703703704						0.0118981481481481						0.0119444444444444						0.0126388888888889];					
y60_r = [NaN						NaN						NaN						NaN	];				
y61_r = [NaN						NaN						NaN						NaN	];				
y62_r = [NaN						NaN						NaN						NaN	];				
y63_r = [0.012962962962963						0.0133333333333333						0.0136574074074074						0.0123611111111111];					
y64_r = [0.0147222222222222						0.0133796296296296						0.0118055555555556						0.00958333333333333	];				
y65_r = [0.0125						0.0163888888888889						0.015						0.00824074074074074	];				
y66_r = [0.0274537037037037						0.0131018518518519						0.0127777777777778						0.00958333333333333	];				
y67_r = [0.167962962962963						0.176712962962963						0.150833333333333						NaN		];			
y68_r = [NaN						NaN						NaN						NaN	];				
y69_r = [NaN						NaN						NaN						NaN	];				
y70_r = [NaN						NaN						NaN						NaN	];				
y71_r = [NaN						0.009						0.0172685185185185						0.0111111111111111];					
y72_r = [0.0174074074074074						0.0146296296296296						0.0164814814814815						0.00861111111111111	];				
y73_r = [0.015						0.0151851851851852						0.0163425925925926						0.0150462962962963	];				
y74_r = [0.0124074074074074						0.0130555555555556						0.0158796296296296						0.00837962962962963];					
y75_r = [0.0126388888888889						0.0148148148148148						0.0125462962962963						0.0105092592592593];					
y76_r = [0.0225925925925926						0.0168518518518519						0.0153240740740741						0.0106944444444444];					
y77_r = [0.0113888888888889						0.0207407407407407						0.0158333333333333						0.01		];			
y78_r = [0.0112037037037037						0.0131944444444444						0.0169444444444444						0.011712962962963];					
y79_r = [0.01375						0.0131018518518519						0.0135185185185185						0.0109259259259259	];				
y80_r = [0.0127314814814815						0.0161111111111111						0.0174537037037037						0.0099537037037037];					
y81_r = [0.015462962962963						0.0166666666666667						0.0165740740740741						0.010787037037037];					
y82_r = [0.0172222222222222						0.0145833333333333						0.01875						0.00939814814814815		];			
y83_r = [0.0210185185185185						0.0160648148148148						0.019212962962963						0.0116666666666667];					
y84_r = [0.0260648148148148						0.0165740740740741						0.0174537037037037						0.00861111111111111];					
y85_r = [0.0130555555555556						0.0185185185185185						0.0159259259259259						0.0111574074074074];					
y86_r = [0.0153703703703704						0.0177314814814815						0.0167592592592593						0.0101851851851852];					
y87_r = [0.0180092592592593						0.0224537037037037						0.0178240740740741						0.00759259259259259	];				
y88_r = [0.0149537037037037						0.0161574074074074						0.01375						0.00916666666666667	];				
y89_r = [0.0128240740740741						0.0148611111111111						0.00324074074074074						NaN	];							

y_values = [
    y1_r; y2_r; y3_r; y4_r; y5_r; y6_r; y7_r; y8_r; y9_r;...
    y10_r; y11_r; y12_r; y13_r; y14_r; y15_r; y16_r; y17_r; y18_r; y19_r;...
    y20_r; y21_r; y22_r; y23_r; y24_r; y25_r; y26_r; y27_r; y28_r; y29_r;...
    y30_r; y31_r; y32_r; y33_r; y34_r; y35_r; y36_r; y37_r; y38_r; y39_r;...
    y40_r; y41_r; y42_r; y43_r; y44_r; y45_r; y46_r; y47_r; y48_r; y49_r;...
    y50_r; y51_r; y52_r; y53_r; y54_r; y55_r; y56_r; y57_r; y58_r; y59_r;...
    y60_r; y61_r; y62_r; y63_r; y64_r; y65_r; y66_r; y67_r; y68_r; y69_r;...
    y70_r; y71_r; y72_r; y73_r; y74_r; y75_r; y76_r; y77_r; y78_r; y79_r;...
    y80_r; y81_r; y82_r; y83_r; y84_r; y85_r; y86_r; y87_r; y88_r; y89_r];

baseline_L = 0.01289;

[N,M] = size(y_values);

group_names = {'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
    'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
    'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
    'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
    'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
    'No Drug Administration', 'No Drug Administration', 'No Drug Administration', 'No Drug Administration',...
    'Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump',...
    'DCZ Pump','DCZ Pump', 'DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump',...
    'No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration',...
    'Saline Injection','Saline Injection','Saline Injection','Saline Injection','Saline Injection',...
    'No Drug Administration','No Drug Administration',...
    'DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection',...
    'No Drug Administration','No Drug Administration','No Drug Administration'};

colors = {"#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
    "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
    "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
    "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
    "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
    "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
    "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
    "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE",...
    "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
    "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120",...
    "#33FFD1", "#33FFD1",...
    "#D133FF", "#D133FF", "#D133FF", "#D133FF", "#D133FF",...
    "#33FFD1", "#33FFD1", "#33FFD1"};

figure(1);
% SEIZURES GRAPH----------------------------------------------

seizures = [0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		1		0		0		0		0		1		1		0		0		0		0		3		5		8		5		9		15		18		2		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0	];

subplot(2,1,1);
hold on;

for i = 1:length(seizures)-1
    plot([i i+1], [seizures(i) seizures(i+1)], 'Color', colors{i}, 'LineWidth', 1.1);
    rgb_color = sscanf(colors{i},'#%2x%2x%2x',[1 3])/255; % Convert hexadecimal color to RGB
    scatter(i, seizures(i), 100, rgb_color, 'filled');
end

rgb_color = sscanf(colors{end},'#%2x%2x%2x',[1 3])/255; % Convert hexadecimal color to RGB
scatter(length(seizures), seizures(end), 100, rgb_color, 'filled');

windowSize = 5; 
b = (1/windowSize)*ones(1,windowSize);
a = 1;
moving_avg = filter(b, a, seizures);

plot(1:length(seizures), moving_avg, 'k', 'LineWidth', 2);

x_indices = 1:N;
xticks(x_indices(9:9:end));
xticklabels({
    '23.11.','2.12.','11.12.','20.12.','29.12.','7.1.',...
    '16.1.','25.1.','3.2.','12.2.', 'Moving Average'});
ax = gca;
ax.FontSize = 13;

h = zeros(length(group_names)+1, 1); 

for i = 1:length(group_names)
    h(i) = scatter(NaN,NaN, 'filled', 'MarkerFaceColor', colors{i}, 'SizeData', 100);
end

h(end) = plot(NaN,NaN, 'k', 'LineWidth', 2);

group_names{end+1} = 'Moving Average';

[unique_group_names, idx] = unique(group_names, 'stable');

legend(h(idx), unique_group_names, 'Location', 'best', 'FontSize', 8);

title('Daily Seizure Frequency (SK000645)', 'FontSize', 18);
xlabel('Date', 'FontSize', 13);
ylabel('Seizure Frequency (n/days)', 'FontSize', 13);

xlim([0.5, length(seizures)+0.5]); 

hold off;


% IED GRAPH-----------------------------------------------------


subplot(2,1,2);
hold on;

h(1) = plot([0.5, N+0.5], [baseline_L, baseline_L], 'Color', [178, 189, 216]/255, 'LineWidth', 2); 

mean_values = nanmean(y_values, 2); 

smooth_mean_values = movmean(mean_values, 5, 'Endpoints', 'shrink');

h(2) = plot(1:N, smooth_mean_values, 'LineWidth', 2, 'Color', 'k');

x_new=linspace(0.75,N+0.5,N*M);
x_new=reshape(x_new,M,[])';

for i=1:size(y_values,1)
    h(i+2) = plot(x_new(i,:),y_values(i,:),'Color',colors{i},'Marker','o','LineStyle','-','MarkerFaceColor',colors{i});
end

x_indices = 1:N;
xticks(x_indices(9:9:end));
xticklabels({
    '23.11.','2.12.','11.12.','20.12.','29.12.','7.1.',...
    '16.1.','25.1.','3.2.','12.2.', 'Baseline', 'Moving Average'});
ax = gca;
ax.FontSize = 13;

group_names{end+1} = 'Baseline';
group_names{end+1} = 'Moving Average'; 
[ugn,idx] = unique(group_names([end-1:end 1:end-2]),'stable');
legend(h(idx),ugn,'Location', 'best', 'FontSize', 8);

title({'IED Frequency during Osmotic Pump','and Injection Drug Administration (SK000645)'}, 'FontSize', 18);
xlabel('Date', 'FontSize', 13);
ylabel('IED Frequency (n/s)', 'FontSize', 13);

xlim([0.5, N+0.5]);

hold off;

drawnow;
pause(10); 

fig = gcf;
fig.PaperUnits = 'inches';
fig.PaperPosition = [0 0 10 8];
fig.PaperSize = [10 8]; 

print('frequency_SK000645.png', '-dpng', '-r600');


%% GRAPH - DETAIL

group_names = {
    'Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump','Saline Pump',...
    'DCZ Pump','DCZ Pump', 'DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump','DCZ Pump',...
    'No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration','No Drug Administration',...
    'Saline Injection','Saline Injection','Saline Injection','Saline Injection','Saline Injection',...
    'No Drug Administration','No Drug Administration',...
    'DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection','DCZ Injection',...
    'No Drug Administration','No Drug Administration','No Drug Administration'};

colors = {
    "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319",...
    "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE",...
    "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
    "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120",...
    "#33FFD1", "#33FFD1",...
    "#D133FF", "#D133FF", "#D133FF", "#D133FF", "#D133FF",...
    "#33FFD1", "#33FFD1", "#33FFD1"};


figure(5);

seizures = [0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0		0	];

subplot(2,1,1);
hold on;

for i = 1:length(seizures)-1
    plot([i i+1], [seizures(i) seizures(i+1)], 'Color', colors{i}, 'LineWidth', 1.1);
    rgb_color = sscanf(colors{i},'#%2x%2x%2x',[1 3])/255; % Convert hexadecimal color to RGB
    scatter(i, seizures(i), 100, rgb_color, 'filled');
end

rgb_color = sscanf(colors{end},'#%2x%2x%2x',[1 3])/255; % Convert hexadecimal color to RGB
scatter(length(seizures), seizures(end), 100, rgb_color, 'filled');

windowSize = 5; 
b = (1/windowSize)*ones(1,windowSize);
a = 1;
moving_avg = filter(b, a, seizures);

plot(1:length(seizures), moving_avg, 'k', 'LineWidth', 2);

x_indices = 1:N;
xticks(x_indices(3:3:end));
xticklabels({
    '10.1.','13.1.','16.1.','19.1.','22.1.','25.1.','28.1.','31.1.',...
    '3.2.','6.2.','9.2.','12.2.', 'Moving Average'});
ax = gca;
ax.FontSize = 13;

h = zeros(length(group_names)+1, 1);

for i = 1:length(group_names)
    h(i) = scatter(NaN,NaN, 'filled', 'MarkerFaceColor', colors{i}, 'SizeData', 100);
end

h(end) = plot(NaN,NaN, 'k', 'LineWidth', 2);

group_names{end+1} = 'Moving Average';

[unique_group_names, idx] = unique(group_names, 'stable');

legend(h(idx), unique_group_names, 'Location', 'best', 'FontSize', 7);

title('Daily Seizure Frequency - detail (SK000645)', 'FontSize', 18);
xlabel('Date', 'FontSize', 13);
ylabel('Seizure Frequency (n/days)', 'FontSize', 13);

xlim([0.5, length(seizures)+0.5]); 

ylim([0 0.5]*1e-4);

hold off;


% IED
y_values = [y55_r; y56_r; y57_r; y58_r; y59_r;...
    y60_r; y61_r; y62_r; y63_r; y64_r; y65_r; y66_r; y67_r; y68_r; y69_r;...
    y70_r; y71_r; y72_r; y73_r; y74_r; y75_r; y76_r; y77_r; y78_r; y79_r;...
    y80_r; y81_r; y82_r; y83_r; y84_r; y85_r; y86_r; y87_r; y88_r; y89_r];

baseline_L = 0.01289;
[N,M] = size(y_values);
subplot(2,1,2);
hold on;

h(1) = plot([0.5, N+0.5], [baseline_L, baseline_L], 'Color', [178, 189, 216]/255, 'LineWidth', 2); 

mean_values = nanmean(y_values, 2); 

smooth_mean_values = movmean(mean_values, 5, 'Endpoints', 'shrink');

h(2) = plot(1:N, smooth_mean_values, 'LineWidth', 2, 'Color', 'k');

x_new=linspace(0.75,N+0.5,N*M);
x_new=reshape(x_new,M,[])';

for i=1:size(y_values,1)
    h(i+2) = plot(x_new(i,:),y_values(i,:),'Color',colors{i},'Marker','o','LineStyle','-','MarkerFaceColor',colors{i});
end

x_indices = 1:N;

xticks(x_indices(3:3:end));
xticklabels({
    '10.1.','13.1.','16.1.','19.1.','22.1.','25.1.','28.1.','31.1.',...
    '3.2.','6.2.','9.2.','12.2.', 'Baseline', 'Moving Average'});
ax = gca;
ax.FontSize = 13;

group_names{end+1} = 'Baseline'; % 
group_names{end+1} = 'Moving Average'; % 
[ugn,idx] = unique(group_names([end-1:end 1:end-2]),'stable');
legend(h(idx),ugn,'Location', 'northeast', 'FontSize', 7);

title({'IED Frequency during Osmotic Pump','and Injection Drug Administration - detail (SK000645)'}, 'FontSize', 18);
xlabel('Date', 'FontSize', 13);
ylabel('IED Frequency (n/s)', 'FontSize', 13);


xlim([0.5, N+0.5]); 

ylim([0 0.05]);


hold off;

drawnow;
pause(10); 

fig = gcf;
fig.PaperUnits = 'inches';
fig.PaperPosition = [0 0 10 8];
fig.PaperSize = [10 8]; 

print('frequency_detail_SK000645.png', '-dpng', '-r600');

