

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',...
    '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',...
    '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',...
    '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'};

y1_r = [0.0132407407407407						0.0192592592592593						0.0135185185185185						0.0101851851851852	];				
y2_r = [0.0118981481481481						0.0172685185185185						0.0176388888888889						0.0143981481481481];					
y3_r = [0.0175						0.0598611111111111						0.0371759259259259						0.0238425925925926	];				
y4_r = [0.0444444444444444						0.0704166666666667						0.0589814814814815						0.0383796296296296	];				
y5_r = [0.0474074074074074						0.0748148148148148						0.0586574074074074						0.0571296296296296	];				
y6_r = [0.0555092592592593						0.0761111111111111						0.0400555555555556						0.0472222222222222	];				
y7_r = [0.0457407407407407						0.0575462962962963						0.0472222222222222						0.0295833333333333	];				
y8_r = [0.0766203703703704						0.0968981481481481						0.102268518518519						0.100972222222222	];				
y9_r = [0.123657407407407						0.12287037037037						0.161898148148148						0.100509259259259	];				
y10_r = [0.101944444444444						0.176296296296296						0.145185185185185						0.0821296296296296	];				
y11_r = [0.0891203703703704						0.105694444444444						0.105324074074074						0.0633333333333333	];				
y12_r = [0.0619907407407407						0.0805092592592593						0.0528333333333333						0.0906944444444444	];				
y13_r = [0.0962037037037037						0.0777777777777778						0.0816203703703704						0.0646296296296296	];				
y14_r = [0.0936111111111111						0.108703703703704						0.105509259259259						0.115416666666667	];				
y15_r = [0.101018518518519						0.143888888888889						0.121296296296296						0.180555555555556	];				
y16_r = [0.169722222222222						0.18037037037037						0.174444444444444						0.146018518518519	];				
y17_r = [0.115324074074074						0.167916666666667						0.113703703703704						0.0968055555555556	];				
y18_r = [0.165231481481481						0.122175925925926						0.11087962962963						0.100185185185185	];				
y19_r = [0.139953703703704						0.0951388888888889						0.10125						0.0917592592592593];					
y20_r = [0.132824074074074						0.11087962962963						0.137037037037037						0.125833333333333	];				
y21_r = [0.165138888888889						0.131388888888889						0.119398148148148						0.0953240740740741	];				
y22_r = [0.131805555555556						0.171018518518519						0.131712962962963						0.0865740740740741	];				
y23_r = [0.111805555555556						0.122638888888889						0.0968055555555556						0.117361111111111	];				
y24_r = [0.0843055555555556						0.092037037037037						0.0878240740740741						0.0739814814814815	];				
y25_r = [0.107453703703704						0.0867592592592593						0.0887962962962963						0.0704166666666667	];				
y26_r = [0.0968981481481481						0.085462962962963						0.115740740740741						0.0990277777777778	];				
y27_r = [0.0994907407407407						0.17837962962963						0.0815277777777778						0.133611111111111	];				
y28_r = [0.121018518518519						0.207546296296296						0.27587962962963						0.124907407407407	];				
y29_r = [0.163287037037037						0.110509259259259						NaN						NaN		];			
y30_r = [NaN						NaN						0.328611111111111						0.265601851851852		];			
y31_r = [0.161064814814815						0.292453703703704						0.17912037037037						0.377638888888889];					
y32_r = [0.216064814814815						0.294907407407407						0.43787037037037						0.298611111111111];					
y33_r = [0.235324074074074						0.28087962962963						0.229722222222222						0.111759259259259];					
y34_r = [0.140277777777778						0.162175925925926						0.146064814814815						0.117361111111111];					
y35_r = [0.0955092592592593						0.113055555555556						0.110138888888889						0.0669907407407407];					
y36_r = [0.0743981481481481						0.072962962962963						0.0836805555555555						NaN	];				
y37_r = [NaN						NaN						0.115208333333333						0.279222222222222];					
y38_r = [0.287407407407407						0.474861111111111						0.402222222222222						0.18712962962963];					
y39_r = [0.339351851851852						0.279722222222222						0.439537037037037						0.144722222222222];					
y40_r = [0.118055555555556						0.211759259259259						0.226388888888889						0.152685185185185];					
y41_r = [0.163703703703704						0.240555555555556						0.215833333333333						0.124907407407407];					
y42_r = [0.119583333333333						0.145462962962963						0.256111111111111						0.100555555555556];					
y43_r = [0.0883333333333333						0.21212962962963						0.197962962962963						0.0896296296296296];					
y44_r = [0.210787037037037						0.166527777777778						0.180925925925926						0.0963425925925926];					
y45_r = [0.148194444444444						0.188333333333333						0.20125						0.123564814814815];					
y46_r = [0.137777777777778						0.274907407407407						0.298333333333333						0.156712962962963];					
y47_r = [0.190833333333333						0.242175925925926						0.222824074074074						0.171666666666667];					
y48_r = [0.149212962962963						0.105648148148148						0.106898148148148						0.0674074074074074];					
y49_r = [0.0598148148148148						0.0436111111111111						0.0468518518518519						0.0263425925925926];					
y50_r = [0.0317592592592593						0.0275462962962963						0.0331481481481481						0.0200462962962963];					
y51_r = [0.0243981481481481						0.0287962962962963						0.0328240740740741						0.0298148148148148];					
y52_r = [0.0517592592592593						0.0679166666666667						0.0707407407407407						0.109722222222222];					
y53_r = [0.125972222222222						0.15625						0.331435185185185						0.160648148148148	];				
y54_r = [0.229768518518519						0.317407407407407						0.241805555555556						0.203935185185185	];				
y55_r = [0.271898148148148						0.491342592592593						0.3325						0.235416666666667];					
y56_r = [0.24375						0.439490740740741						0.308101851851852						0.201898148148148];					
y57_r = [0.239722222222222						0.312175925925926						0.243842592592593						0.204398148148148];					
y58_r = [0.159768518518519						0.32212962962963						0.244814814814815						0.197314814814815];					
y59_r = [0.15875						0.346990740740741						0.249212962962963						0.135555555555556];					
y60_r = [0.118194444444444						0.183703703703704						0.208194444444444						0.13875	];				
y61_r = [0.204305555555556						0.276064814814815						0.307546296296296						0.175509259259259];					
y62_r = [0.240231481481481						0.358657407407407						0.382685185185185						0.276342592592593];					
y63_r = [0.186296296296296						0.33037037037037						0.336990740740741						0.144537037037037];					
y64_r = [0.13412037037037						0.177222222222222						NaN						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];

[N,M] = size(y_values);

baseline_L = 0.0842;

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

figure(1);

% SEIZURES GRAPH----------------------------------------------

seizures = [0		3		2		2		1		0		1		4		2		1		1		0		0		3		4		2		0		1		1		2		1		3		1		1		0		2		2		4		1		0		11		8		8		1		0		0		3		7		6		1		6		7		3		6		9		16		16		0		0		0		0		1		5		8		17		14		7		21		13		12		19		18		2		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
% Last circle point
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');

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

% Plot moving average
plot(1:length(seizures), moving_avg, 'k', 'LineWidth', 2);

% Set x-axis labels
x_indices = 1:N;
% xticks(x_indices(7:7:end)); % Placing x-axis ticks for every seventh value
% xticklabels({ '6.12.', '13.2.', '20.2.', '27.2.', '6.3.', '13.3.', '20.3.',...
%     '27.3.', '3.4.', 'Moving Average'}); % Adjusted x-axis labels

xticks(x_indices(6:6:end)); 
xticklabels({ '5.2.', '11.2.','17.2.','23.2.','29.2.',...
    '6.3.', '12.3.', '18.3.','24.3.','30.3.','31.3.', ...
    'Moving Average'});
ax = gca;
ax.FontSize = 13;

% Create a dummy plot for legend
h = zeros(length(group_names)+1, 1); % Increase the length by 1 for "Moving Average"

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

% Plot for "Moving Average"
h(end) = plot(NaN,NaN, 'k', 'LineWidth', 2);

% Add "Moving Average" to group_names
group_names{end+1} = 'Moving Average';

% Get unique group names and their indices
[unique_group_names, idx] = unique(group_names, 'stable');

% Add legend
legend(h(idx), unique_group_names, 'Location', 'northwest', 'FontSize', 8);

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

% Set x-axis limits
xlim([0.5, length(seizures)+0.5]); % Setting xlim for subplot 1

hold off;


% IED GRAPH-----------------------------------------------------
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',...
    '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',...
    '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',...
    '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'};

y1_r = [0.0132407407407407						0.0192592592592593						0.0135185185185185						0.0101851851851852	];				
y2_r = [0.0118981481481481						0.0172685185185185						0.0176388888888889						0.0143981481481481];					
y3_r = [0.0175						0.0598611111111111						0.0371759259259259						0.0238425925925926	];				
y4_r = [0.0444444444444444						0.0704166666666667						0.0589814814814815						0.0383796296296296	];				
y5_r = [0.0474074074074074						0.0748148148148148						0.0586574074074074						0.0571296296296296	];				
y6_r = [0.0555092592592593						0.0761111111111111						0.0400555555555556						0.0472222222222222	];				
y7_r = [0.0457407407407407						0.0575462962962963						0.0472222222222222						0.0295833333333333	];				
y8_r = [0.0766203703703704						0.0968981481481481						0.102268518518519						0.100972222222222	];				
y9_r = [0.123657407407407						0.12287037037037						0.161898148148148						0.100509259259259	];				
y10_r = [0.101944444444444						0.176296296296296						0.145185185185185						0.0821296296296296	];				
y11_r = [0.0891203703703704						0.105694444444444						0.105324074074074						0.0633333333333333	];				
y12_r = [0.0619907407407407						0.0805092592592593						0.0528333333333333						0.0906944444444444	];				
y13_r = [0.0962037037037037						0.0777777777777778						0.0816203703703704						0.0646296296296296	];				
y14_r = [0.0936111111111111						0.108703703703704						0.105509259259259						0.115416666666667	];				
y15_r = [0.101018518518519						0.143888888888889						0.121296296296296						0.180555555555556	];				
y16_r = [0.169722222222222						0.18037037037037						0.174444444444444						0.146018518518519	];				
y17_r = [0.115324074074074						0.167916666666667						0.113703703703704						0.0968055555555556	];				
y18_r = [0.165231481481481						0.122175925925926						0.11087962962963						0.100185185185185	];				
y19_r = [0.139953703703704						0.0951388888888889						0.10125						0.0917592592592593];					
y20_r = [0.132824074074074						0.11087962962963						0.137037037037037						0.125833333333333	];				
y21_r = [0.165138888888889						0.131388888888889						0.119398148148148						0.0953240740740741	];				
y22_r = [0.131805555555556						0.171018518518519						0.131712962962963						0.0865740740740741	];				
y23_r = [0.111805555555556						0.122638888888889						0.0968055555555556						0.117361111111111	];				
y24_r = [0.0843055555555556						0.092037037037037						0.0878240740740741						0.0739814814814815	];				
y25_r = [0.107453703703704						0.0867592592592593						0.0887962962962963						0.0704166666666667	];				
y26_r = [0.0968981481481481						0.085462962962963						0.115740740740741						0.0990277777777778	];				
y27_r = [0.0994907407407407						0.17837962962963						0.0815277777777778						0.133611111111111	];				
y28_r = [0.121018518518519						0.207546296296296						0.27587962962963						0.124907407407407	];				
y29_r = [0.163287037037037						0.110509259259259						NaN						NaN		];			
y30_r = [NaN						NaN						0.328611111111111						0.265601851851852		];			
y31_r = [0.161064814814815						0.292453703703704						0.17912037037037						0.377638888888889];					
y32_r = [0.216064814814815						0.294907407407407						0.43787037037037						0.298611111111111];					
y33_r = [0.235324074074074						0.28087962962963						0.229722222222222						0.111759259259259];					
y34_r = [0.140277777777778						0.162175925925926						0.146064814814815						0.117361111111111];					
y35_r = [0.0955092592592593						0.113055555555556						0.110138888888889						0.0669907407407407];					
y36_r = [0.0743981481481481						0.072962962962963						0.0836805555555555						NaN	];				
y37_r = [NaN						NaN						0.115208333333333						0.279222222222222];					
y38_r = [0.287407407407407						0.474861111111111						0.402222222222222						0.18712962962963];					
y39_r = [0.339351851851852						0.279722222222222						0.439537037037037						0.144722222222222];					
y40_r = [0.118055555555556						0.211759259259259						0.226388888888889						0.152685185185185];					
y41_r = [0.163703703703704						0.240555555555556						0.215833333333333						0.124907407407407];					
y42_r = [0.119583333333333						0.145462962962963						0.256111111111111						0.100555555555556];					
y43_r = [0.0883333333333333						0.21212962962963						0.197962962962963						0.0896296296296296];					
y44_r = [0.210787037037037						0.166527777777778						0.180925925925926						0.0963425925925926];					
y45_r = [0.148194444444444						0.188333333333333						0.20125						0.123564814814815];					
y46_r = [0.137777777777778						0.274907407407407						0.298333333333333						0.156712962962963];					
y47_r = [0.190833333333333						0.242175925925926						0.222824074074074						0.171666666666667];					
y48_r = [0.149212962962963						0.105648148148148						0.106898148148148						0.0674074074074074];					
y49_r = [0.0598148148148148						0.0436111111111111						0.0468518518518519						0.0263425925925926];					
y50_r = [0.0317592592592593						0.0275462962962963						0.0331481481481481						0.0200462962962963];					
y51_r = [0.0243981481481481						0.0287962962962963						0.0328240740740741						0.0298148148148148];					
y52_r = [0.0517592592592593						0.0679166666666667						0.0707407407407407						0.109722222222222];					
y53_r = [0.125972222222222						0.15625						0.331435185185185						0.160648148148148	];				
y54_r = [0.229768518518519						0.317407407407407						0.241805555555556						0.203935185185185	];				
y55_r = [0.271898148148148						0.491342592592593						0.3325						0.235416666666667];					
y56_r = [0.24375						0.439490740740741						0.308101851851852						0.201898148148148];					
y57_r = [0.239722222222222						0.312175925925926						0.243842592592593						0.204398148148148];					
y58_r = [0.159768518518519						0.32212962962963						0.244814814814815						0.197314814814815];					
y59_r = [0.15875						0.346990740740741						0.249212962962963						0.135555555555556];					
y60_r = [0.118194444444444						0.183703703703704						0.208194444444444						0.13875	];				
y61_r = [0.204305555555556						0.276064814814815						0.307546296296296						0.175509259259259];					
y62_r = [0.240231481481481						0.358657407407407						0.382685185185185						0.276342592592593];					
y63_r = [0.186296296296296						0.33037037037037						0.336990740740741						0.144537037037037];					
y64_r = [0.13412037037037						0.177222222222222						NaN						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];

baseline_L = 0.0842;

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

% Baseline
h(1) = plot([0.5, N+0.5], [baseline_L, baseline_L], 'Color', [178, 189, 216]/255, 'LineWidth', 2); % Black line with thicker width

% Calculate the mean for each date
mean_values = nanmean(y_values, 2); % Using nanmean to handle NaN values

% A moving average with a window size of 5
smooth_mean_values = movmean(mean_values, 5, 'Endpoints', 'shrink');

% Plot the smooth line
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

% Set x-axis labels
x_indices = 1:N;
% xticks(x_indices(7:7:end)); % Placing x-axis ticks for every seventh value
% xticklabels({ '6.12.', '13.2.', '20.2.', '27.2.', '6.3.',...
%     '13.3.', '20.3.', '27.3.', '3.4.', 'Baseline', 'Moving Average'}); % Adjusted x-axis labels

xticks(x_indices(6:6:end)); 
xticklabels({ '5.2.', '11.2.','17.2.','23.2.','29.2.',...
    '6.3.', '12.3.', '18.3.','24.3.','30.3.','31.3.', ...
    'Baseline', 'Moving Average'});
ax = gca;
ax.FontSize = 13;

% Add legend
group_names{end+1} = 'Baseline'; % Add baseline to the legend
group_names{end+1} = 'Moving Average'; % Add moving average to the legend
[ugn,idx] = unique(group_names([end-1:end 1:end-2]),'stable');
legend(h(idx), ugn, 'Location', 'northwest', 'FontSize', 8);

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

% Set x-axis limits
xlim([0.5, N+0.5]); % Setting xlim for subplot 2

hold off;

% Ensure figure is fully rendered before saving
drawnow;
pause(10); % Short pause to ensure rendering is complete

% Set the size of the figure
fig = gcf;
fig.PaperUnits = 'inches';
fig.PaperPosition = [0 0 10 8];
fig.PaperSize = [10 8]; % Ensure PaperSize matches PaperPosition

% Save the plot as an image
print('frequency_SK001071.png', '-dpng', '-r600');