
y1_r = [NaN						0.0275						0.0459722222222222						0.0433796296296296	];				
y2_r = [0.0460185185185185						0.0371759259259259						0.0456481481481481						0.042962962962963];					
y3_r = [0.0464814814814815						0.0400925925925926						0.0454166666666667						0.0303703703703704];					
y4_r = [0.0497685185185185						0.0574537037037037						0.0500925925925926						0.0409722222222222];					
y5_r = [0.0523611111111111						0.047037037037037						0.0405555555555556						0.0330092592592593];					
y6_r = [0.0671296296296296						0.0424537037037037						0.0221759259259259						NaN	];				
y7_r = [NaN						NaN						0.0244444444444444						NaN	];				
y8_r = [NaN						0.0313888888888889						0.066712962962963						0.0362037037037037];					
y9_r = [0.0624074074074074						0.0613425925925926						0.0478703703703704						0.037962962962963];					
y10_r = [0.0597685185185185						0.0483333333333333						0.0752777777777778						0.0293981481481481];					
y11_r = [0.0550925925925926						0.0508333333333333						0.0556481481481481						0.0191203703703704];					
y12_r = [0.0283333333333333						0.0523611111111111						0.0531018518518519						0.0331944444444444];					
y13_r = [0.0238425925925926						0.0490740740740741						0.0515740740740741						0.0190277777777778];					
y14_r = [0.0275						0.0555092592592593						0.051712962962963						0.0231018518518519];					
y15_r = [0.0264351851851852						0.0518055555555556						0.0483333333333333						0.0155555555555556];					
y16_r = [0.0302314814814815						0.0527777777777778						0.0517592592592593						0.0116203703703704];					
y17_r = [0.0385185185185185						0.0388425925925926						0.0540277777777778						0.0175925925925926];					
y18_r = [0.0350462962962963						0.04875						0.0452314814814815						0.0213888888888889];					
y19_r = [0.0461111111111111						0.0535648148148148						0.0430555555555556						0.0272685185185185];					
y20_r = [0.03						0.0538425925925926						0.0406481481481481						0.0228240740740741	];				
y21_r = [0.0384722222222222						0.0511574074074074						0.0416203703703704						0.0152314814814815];					
y22_r = [0.0248611111111111						0.0453703703703704						0.0497685185185185						0.0290277777777778];					
y23_r = [0.0393518518518519						0.0302777777777778						0.0473148148148148						0.0202777777777778];					
y24_r = [0.0460185185185185						0.0374537037037037						0.0469907407407407						0.0271759259259259];					
y25_r = [0.0440740740740741						0.0542592592592593						0.0385185185185185						0.0349537037037037];					
y26_r = [0.029537037037037						0.0512962962962963						0.0520833333333333						0.0238888888888889];					
y27_r = [0.0345833333333333						0.0475925925925926						0.0322685185185185						0.0334722222222222];					
y28_r = [0.0423611111111111						0.045						0.0341203703703704						0.0289814814814815	];				
y29_r = [0.0486111111111111						0.0446759259259259						0.0445833333333333						0.0394907407407407];					
y30_r = [0.0484722222222222						0.0378703703703704						0.0608333333333333						0.0525462962962963	];				
y31_r = [0.0890277777777778						0.109675925925926						0.000277777777777778						0.000277777777777778];					
y32_r = [0.00217592592592593						0.00458333333333333						0.00412037037037037						0.00875	];				
y33_r = [0.0259259259259259						0.0234259259259259						0.0261574074074074						0.0219444444444444];					
y34_r = [0.0216666666666667						0.111481481481481						0.0734722222222222						0.03		];			
y35_r = [0.0368055555555556						0.0490740740740741						0.0394444444444444						0.0200925925925926];					
y36_r = [0.032962962962963						0.064212962962963						0.052037037037037						0.0487962962962963	];				
y37_r = [0.0672222222222222						0.0950925925925926						0.0611574074074074						0.0570833333333333	];				
y38_r = [0.0769444444444444						0.06625						0.0794444444444444						0.125138888888889		];			
y39_r = [0.13037037037037						0.159675925925926						0.141712962962963						0.137175925925926];					
y40_r = [0.0727314814814815						0.140185185185185						0.104027777777778						NaN	];				
y41_r = [NaN						NaN						NaN						NaN	];				
y42_r = [NaN						NaN						NaN						NaN	];				
y43_r = [NaN						NaN						NaN						NaN	];				
y44_r = [NaN						0.0208333333333333						0.100138888888889						0.116157407407407];					
y45_r = [0.143333333333333						0.150740740740741						0.18625						0.207546296296296];					
y46_r = [0.181203703703704						0.233055555555556						0.183518518518519						0.205833333333333];					
y47_r = [0.179537037037037						0.247407407407407						0.194351851851852						0.165601851851852];					
y48_r = [0.115694444444444						0.0993518518518519						0.392361111111111						0.288009259259259];					
y49_r = [0.349675925925926						0.343842592592593						0.25587962962963						0.204074074074074];					
y50_r = [0.278935185185185						0.2775						0.199861111111111						0.16287037037037];					
y51_r = [0.183564814814815						0.137962962962963						0.194675925925926						0.143194444444444];					
y52_r = [0.115509259259259						0.174074074074074						0.125046296296296						0.184490740740741];					
y53_r = [0.15375						0.219305555555556						0.169953703703704						0.115509259259259];					
y54_r = [0.0819444444444444						0.159212962962963						0.15962962962963						0.109398148148148];					
y55_r = [0.203842592592593						0.0963888888888889						0.120185185185185						0.0547685185185185];					
y56_r = [0.0343055555555556						0.238472222222222						0.182407407407407						0.203194444444444];					
y57_r = [0.292314814814815						0.276203703703704						0.236296296296296						0.156203703703704];					
y58_r = [0.189074074074074						0.206666666666667						0.163055555555556						0.0887962962962963];					
y59_r = [0.133009259259259						0.131342592592593						0.149768518518519						0.125];					
y60_r = [0.145324074074074						0.165277777777778						0.264814814814815						0.151527777777778];					
y61_r = [0.212453703703704						0.220416666666667						0.291712962962963						0.14462962962963];					
y62_r = [0.246203703703704						0.189444444444444						0.305925925925926						0.136666666666667];					
y63_r = [0.208657407407407						0.249953703703704						0.207777777777778						0.166574074074074];					
y64_r = [0.1695						0.132055555555556						0.212111111111111						0.119888888888889];					
y65_r = [0.150601851851852						0.162592592592593						0.246388888888889						0.14287037037037];					
y66_r = [0.199675925925926						0.230509259259259						0.293287037037037						0.209537037037037];					
y67_r = [0.224907407407407						0.267314814814815						0.231805555555556						0.201296296296296];					
y68_r = [0.17125						0.269166666666667						0.182962962962963						0.126435185185185];					
y69_r = [0.171157407407407						0.123935185185185						0.114259259259259						0.106388888888889];					
y70_r = [0.228564814814815						0.205787037037037						0.25837962962963						0.177268518518519];					
y71_r = [0.346157407407407						0.296805555555556						0.355						0.2775];					
y72_r = [0.333009259259259						0.295138888888889						0.240231481481481						0.222546296296296];					
y73_r = [0.355185185185185						0.295185185185185						0.329398148148148						0.17875	];				
y74_r = [0.37962962962963						0.379166666666667						0.360648148148148						0.32662037037037];					
y75_r = [0.27912037037037						0.396666666666667						0.314259259259259						0.264027777777778];					
y76_r = [0.149444444444444						0.240046296296296						0.146481481481481						0.231064814814815];					
y77_r = [0.236388888888889						0.331111111111111						0.593425925925926						0.406666666666667];					
y78_r = [0.398425925925926						0.373194444444444						0.414259259259259						0.240092592592593];					
y79_r = [0.247037037037037						0.307361111111111						0.395972222222222						0.236527777777778];					
y80_r = [0.231759259259259						0.279953703703704						0.294722222222222						0.151805555555556];					
y81_r = [0.310925925925926						0.453101851851852						0.422407407407407						0.241481481481481];					
y82_r = [0.341851851851852						0.242453703703704						0.433055555555556						0.417037037037037];					
y83_r = [0.218796296296296						0.317175925925926						0.340324074074074						0.135509259259259];					
y84_r = [0.230833333333333						0.390185185185185						0.35212962962963						0.252222222222222];					
y85_r = [0.422175925925926						0.192777777777778						0.389166666666667						0.140601851851852];					
y86_r = [0.363611111111111						0.236064814814815						0.265787037037037						0.148333333333333];					
y87_r = [0.406851851851852						0.137407407407407						0.500601851851852						0.119814814814815];					
y88_r = [0.272314814814815						0.266111111111111						0.383148148148148						0.206898148148148];					
y89_r = [0.140740740740741						0.251435185185185						0.278425925925926						0.30125		];			
y90_r = [0.105601851851852						0.126296296296296						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; 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;...
    y90_r];

baseline_L = 0.0421;

[N,M] = size(y_values);

% GRAPH PLOTTING
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",...
    "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#4DBEEE", "#D95319",...
    "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#D95319", "#EDB120", "#EDB120",...
    "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1", "#33FFD1",...
    "#D133FF", "#D133FF", "#D133FF", "#D133FF", "#D133FF",...
    "#33FFD1", "#33FFD1",...
    "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120",...
    "#33FFD1", "#33FFD1",...
    "#D133FF", "#D133FF", "#D133FF", "#D133FF", "#D133FF",...
    "#33FFD1", "#33FFD1",...
    "#EDB120", "#EDB120", "#EDB120", "#EDB120", "#EDB120",...
    "#33FFD1", "#33FFD1",...
    "#D133FF"};


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

seizures = [0		0		0		0		0		0		0		0		0		0		0		0		0		0		1		0		1		0		1		1		0		0		0		1		1		0		1		0		1		1		1		0		0		0		1		4		4		4		2		1		0		0		0		2		6		5		4		6		6		4		2		3		2		2		2		8		8		6		8		9		16		13		15		7		17		18		18		9		8		8		15		16		17		14		15		10		16		2		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
% 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');

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(10:10:end));
xticklabels({'21.12.','31.12.','10.1.','20.1.','30.1.','9.2.','19.2.',...
    '29.2.','10.3.', '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', 'northwest', 'FontSize', 8);

title('Daily Seizure Frequency  (SK000918)', '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(10:10:end));
xticklabels({'21.12.','31.12.','10.1.','20.1.','30.1.','9.2.','19.2.',...
    '29.2.','10.3.', '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', 'northwest', 'FontSize', 8);

title({'IED Frequency during Osmotic Pump','and Injection Drug Administration (SK000918)'}, '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_SK000918.png', '-dpng', '-r600');