Commit f5911223 authored by Jesse Heckman's avatar Jesse Heckman

major update draft + graphics

parent a5e77167
......@@ -22,7 +22,7 @@ function draft(obj)
set(t,'FontSize',20);
ax = gobjects(0);
dsz = size(obj);
objsz = size(obj);
%% Super labels
% Check if labels are the same, and prep super labels
......@@ -34,35 +34,50 @@ function draft(obj)
end
obj(1).labels.supx = lab_x;
obj(1).labels.supy = lab_y;
%% Create (sub)plots
% Draft each subplot
% Select compare axis.
objsz = numel(obj);
cmpsz = 1;
tmpV = [];
if isfield(obj(1).pva,'axcomp'); tmpV = obj(1).pva.axcomp; end
if ~isempty(tmpV) && objsz == 1; cmpsz = length(unique(tmpV.feature)); end
%% Set Compare axis
cmp = length(unique(obj(1).pva.axcomp.feature));
cmpsz = [1 1];
nObj = numel(obj)*cmp;
if min(objsz)>1
cmp = 1;
else
if objsz(1)<objsz(2)
% horizontal direction
cmpsz(1) = cmp;
else
% vertical direction
cmpsz(2) = cmp;
end
end
for iCmp = 1:cmpsz
% Make Compare Axes
% // NOTE THAT THIS IS VERY INEFFICIENT: DO 1) MAKE AXCMP DEFAULT 1
r = 1;
c = 1;
ax(iCmp) = subplot(r,c,iCmp);
for iObj = 1:objsz
%% Create (sub)plots
% Draft each subplot
for iObj = 1:numel(obj)
% Repeat for comparing axis
for iCmp = 1:cmp
cObj = (iObj-1)*cmp + iCmp;
% Make Axes
iAx = pb_invidx([dsz(1), dsz(2)],iObj);
ax(iAx) = subplot(dsz(1),dsz(2),iObj);
Ax = pb_invidx([objsz(1)*cmpsz(1), objsz(2)*cmpsz(2)],cObj); % reverse get axes index
Ax2Obj = pb_invidx([objsz(1), objsz(2)],iObj); % reverse get object index
ax(Ax) = subplot(objsz(1)*cmpsz(1),objsz(2)*cmpsz(2),cObj); % make axis
if obj(iAx).pva.subtitle; title(obj(iAx).pva.subtitle); end
axis(obj(iAx).pva.axis);
if obj(iObj).pva.subtitle; title(obj(Ax2Obj).pva.subtitle); end
axis(obj(Ax2Obj).pva.axis);
hold on;
% Select data
uFeat = unique(obj(1).pva.axcomp.feature);
sel = obj(1).pva.axcomp.feature == uFeat(iCmp);
D = obj(Ax2Obj).pva;
D.x = D.x(sel);
D.y = D.y(sel);
D.color = D.color(sel);
% Set colors
nCol = 1;
if ~obj(iObj).pva.continious
......@@ -71,17 +86,16 @@ function draft(obj)
% Plot all graphs
for iDP = 1:length(obj(iObj).dplot)
d = obj(iAx).pva;
obj(iObj).dplot{iDP}(obj,d);
obj(iObj).dplot{iDP}(obj,D);
end
% Set labels
if ~obj(1).labels.supx; xlabel(obj(iAx).labels.xlab); end
if ~obj(1).labels.supy; ylabel(obj(iAx).labels.ylab); end
if ~obj(1).labels.supx; xlabel(obj(Ax2Obj).labels.xlab); end
if ~obj(1).labels.supy; ylabel(obj(Ax2Obj).labels.ylab); end
% Make scientific notations on the axes.
ax(iAx).YAxis.Exponent = length(num2str(max(abs(round(ax(iAx).YLim)))))-1;
ax(iAx).XAxis.Exponent = length(num2str(max(abs(round(ax(iAx).XLim)))))-1;
ax(Ax).YAxis.Exponent = length(num2str(max(abs(round(ax(Ax).YLim)))))-1;
ax(Ax).XAxis.Exponent = length(num2str(max(abs(round(ax(Ax).XLim)))))-1;
end
end
......@@ -91,8 +105,8 @@ function draft(obj)
% TO DO:
% 1. Optionalize subplots to have squared/linked/fixed axis
% 2. Scale and move axis to make graph nice, pleasing and non-overlapping
make_axes(obj,ax);
make_suplabel(obj);
obj.make_axes(ax);
obj.make_suplabel;
end
......@@ -101,17 +115,4 @@ end
% Part of Programmeer Beer Toolbox (PBToolbox) %
% Written by: Jesse J. Heckman (2019) %
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % FIX COLOR REPEAT ONLY FOR COLOR SPLITTING DATA
% colors = pb_selectcolor(nCol,obj(iAx).pva.def);
% uD = unique(obj(iAx).pva.color);
% for iCol = 1:nCol
% d = obj(iAx).pva;
% d.ind = d.color == uD(iCol);
% d.color = colors(iCol,:);
% end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
\ No newline at end of file
function draft(obj)
% PB_DRAFT>DRAFT
%
% OBJ.DRAFT will draw a figure from a pb_draft-object.
%
% See also PB_DRAFT, SET_LABELS, SET_TITLE, SET_GRID, PRINT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl
%% Create figure
% Select parent/make figure
if isempty(obj(1).parent)
for iObj = 1:numel(obj)
obj(iObj).parent = gcf;
end
end
% Set figure settings
set(obj(1).parent,'color','w');
t = sgtitle(obj(1).parent,obj(1).title);
set(t,'FontSize',20);
ax = gobjects(0);
dsz = size(obj);
%% Super labels
% Check if labels are the same, and prep super labels
lab_x = true; lab_y = true;
for iObj = 1:numel(obj)
if ~strcmp(obj(1).labels.xlab, obj(iObj).labels.xlab); lab_x = false; end
if ~strcmp(obj(1).labels.ylab, obj(iObj).labels.ylab); lab_y = false; end
end
obj(1).labels.supx = lab_x;
obj(1).labels.supy = lab_y;
%% Create (sub)plots
% Draft each subplot
% Create comparing axis
cmp = length(unique(obj(1).pva.axcomp.feature));
if min(dsz)>1
cmp = 1;
if dsz(1)>dsz(2)
obj(1).pva.axcomp.orientation = 'vertical';
else
obj(1).pva.axcomp.orientation = 'horizontal';
end
end
nObj = numel(obj)*cmp;
for iObj = 1:numel(obj)
% Repeat for comparing axis
for iCmp = 1:cmp
% Make Axes
iAx = pb_invidx([dsz(1), dsz(2)],iObj);
ax(iAx) = subplot(dsz(1),dsz(2),iObj);
if obj(iAx).pva.subtitle; title(obj(iAx).pva.subtitle); end
axis(obj(iAx).pva.axis);
hold on;
% Set colors
nCol = 1;
if ~obj(iObj).pva.continious
nCol = length(unique(obj(iObj).pva.color));
end
% Plot all graphs
for iDP = 1:length(obj(iObj).dplot)
d = obj(iAx).pva;
obj(iObj).dplot{iDP}(obj,d);
end
% Set labels
if ~obj(1).labels.supx; xlabel(obj(iAx).labels.xlab); end
if ~obj(1).labels.supy; ylabel(obj(iAx).labels.ylab); end
% Make scientific notations on the axes.
ax(iAx).YAxis.Exponent = length(num2str(max(abs(round(ax(iAx).YLim)))))-1;
ax(iAx).XAxis.Exponent = length(num2str(max(abs(round(ax(iAx).XLim)))))-1;
end
end
%% Adjust axis handles
% Set super labels
% TO DO:
% 1. Optionalize subplots to have squared/linked/fixed axis
% 2. Scale and move axis to make graph nice, pleasing and non-overlapping
make_axes(obj,ax);
make_suplabel(obj);
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %
% Part of Programmeer Beer Toolbox (PBToolbox) %
% Written by: Jesse J. Heckman (2019) %
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
\ No newline at end of file
......@@ -25,6 +25,7 @@ classdef pb_draft < matlab.mixin.Copyable
pva;
labels;
grid;
dplot = {};
......
......@@ -63,7 +63,7 @@ function h = bubble_plot(~,data,p)
x = x(sel);
y = y(sel);
SZ = ceil(100*M);
SZ = ceil(50*M);
[~,~,idx] = unique(M);
col = statcolor(max(idx),[],[],[],'def',p.def);
C = col(idx,:);
......
......@@ -5,9 +5,10 @@ function plot_dline(obj,varargin)
%
% See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl
% PBToolbox (2019): JJH: j.heckman@donders.ru.nl
p.v = varargin;
p.lim = [obj(1).pva.limits.x obj(1).pva.limits.y];
obj.dplot = vertcat(obj.dplot,{@(dobj,data)dline(dobj,data,p)});
end
......@@ -17,7 +18,7 @@ function h = dline(~,~,p)
style = pb_keyval('style',p.v,'k--');
visibility = pb_keyval('visibility',p.v,'off');
h = pb_dline('style',style,'visibility',visibility);
h = pb_dline('style',style,'lim',p.lim,'visibility',visibility);
end
......
......@@ -5,9 +5,10 @@ function plot_hline(obj,varargin)
%
% See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl
% PBToolbox (2019): JJH: j.heckman@donders.ru.nl
p.v = varargin;
p.lim = obj(1).pva.limits.x;
obj.dplot = vertcat(obj.dplot,{@(dobj,data)hline(dobj,data,p)});
end
......@@ -28,7 +29,7 @@ function h = hline(~,data,p)
point = mode(data.y);
end
end
h = pb_hline(point,'style',style);
h = pb_hline(point,'lim',p.lim,'style',style);
end
......
......@@ -29,9 +29,9 @@ function h = rawdata(~,data,p,colorindex)
%% MAKE SELECTION FOR COLOR!
sel = data.color == colorindex;
x = data.x(sel);
y = data.y(sel);
sel = data.color == colorindex;
x = data.x(sel);
y = data.y(sel);
color = pb_selectcolor(length(p.ncol),data.def);
......
......@@ -5,9 +5,10 @@ function plot_vline(obj,varargin)
%
% See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl
% PBToolbox (2019): JJH: j.heckman@donders.ru.nl
p.v = varargin;
p.lim = obj(1).pva.limits.y;
obj.dplot = vertcat(obj.dplot,{@(dobj,data)vline(dobj,data,p)});
end
......@@ -28,7 +29,7 @@ function h = vline(~,data,p)
point = mode(data.x);
end
end
h = pb_vline(point,'style',style);
h = pb_vline(point,'lim',p.lim,'style',style);
end
......
......@@ -14,11 +14,12 @@ function print(obj,varargin)
fn = pb_keyval('fn',v,defaultname(cdir));
fit = pb_keyval('fit',v,'-bestfit');
format = pb_keyval('format',v,'-dpdf');
render = pb_keyval('render',v,'-painters');
disp = pb_keyval('disp',v,false);
figure(h);
loc = [cdir fn];
print(fit,loc,format);
print(fit,loc,format,render);
if disp
open([loc '.' format(3:end)]);
......
function make_axes(obj,ax,varargin)
% PB_DRAFT>SET_LEGEND
% PB_DRAFT>MAKE_AXES
%
% OBJ.SET_LEGEND(varargin) rescale, size and store axes handles..
%
% See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl
% PBToolbox (2019): JJH: j.heckman@donders.ru.nl
obj(1).h_ax_plot = ax;
% TODO: LINK AXES AND FIND OPTIMAL AXE TICKS
linkaxes(ax,'xy');
positions = zeros(length(ax),4);
for iAx = 1:length(ax)
axis(ax(iAx));
xt = xticks;
yt = yticks;
setAx = ax(iAx);
xlim(ax(iAx),[xt(1) xt(end)]);
ylim(ax(iAx),[yt(1) yt(end)]);
positions(iAx,:) = ax(iAx).Position(:);
if obj(1).grid.bool
f = obj(1).grid.features;
setAx.Box = f.Box;
setAx.TickDir = f.TickDir;
setAx.TickLength = f.TickLength;
setAx.XMinorTick = f.XMinorTick;
setAx.YMinorTick = f.YMinorTick;
setAx.YGrid = f.YGrid;
setAx.XGrid = f.XGrid;
setAx.YColor = f.YColor;
setAx.XColor = f.XColor;
setAx.FontSize = f.FontSize;
setAx.YDir = f.YDir;
setAx.LineWidth = f.LineWidth;
end
end
obj(1).labels.ypos = min(positions(:,2));
obj(1).labels.xpos = min(positions(:,1));
obj(1).labels.pos = [min(positions(:,1))/1.33 min(positions(:,2))/1.33 max(positions(:,1)+positions(:,3)) max(positions(:,2)+positions(:,4))];
obj(1).h_ax_plot = ax;
end
......
function make_suplabel(obj,varargin)
% PB_DRAFT>MAKE_SUPLABEL
%
% OBJ.SET_LEGEND(varargin) Make superlabels handle.
% OBJ.MAKE_SUPLABEL(varargin) make superlabels handle.
%
% See also PB_DRAFT
......
......@@ -11,20 +11,36 @@ function parse_va(obj,varargin)
%% READ DATA
% Read Data, and set keyvals
% Read keyvals
v = varargin;
pva.x = pb_keyval('x',v,[]);
pva.y = pb_keyval('y',v,[]);
pva.z = pb_keyval('z',v,[]);
pva.def = pb_keyval('def',v,1);
pva.color = pb_keyval('color',v,ones(length(pva.x),1));
pva.axis = pb_keyval('axis',v,'square');
pva.axis = pb_keyval('axis',v);
pva.subtitle = pb_keyval('subtitle',v);
pva.linkax = pb_keyval('link',v,true);
% Check data limits
[x,y] = setlim(pva);
pva.limits.x = x;
pva.limits.y = y;
% Check square axis
if isempty(pva.axis)
if abs(max(x)-max(y)) / max([x,y])<.5
pva.axis = 'square';
end
end
% Set layout
pva.colscheme = pb_selectcolor(length(unique(pva.color)),pva.def);
pva.axcomp = axcmp(pva.x);
obj.labels.xlab = pb_keyval('xlab',v,'');
obj.labels.ylab = pb_keyval('ylab',v,'');
obj.grid.bool = false;
% Check data continuity for colouring
pva.continious = false;
......@@ -35,6 +51,41 @@ function parse_va(obj,varargin)
obj.pva = pva;
end
function ac = axcmp(data)
% Writes empty axcomp data struct
ac.prefix = '';
ac.feature = ones(1,length(data));
ac.n = length(unique(ac.feature));
end
function [x,y] = setlim(data)
% Sets the limits for axes
x = [min(data.x) max(data.x)];
y = [min(data.y) max(data.y)];
xr = x(2)-x(1);
yr = y(2)-y(1);
% minimum
if abs(min(x))/xr < .1; x(1) = 0; end
if abs(min(y))/yr < .1; y(1) = 0; end
% maximum
tmp = max([x,y]);
order = 0;
scale = .1;
if tmp<1; scale = 10; end
while tmp < .1 || tmp > 1
tmp = tmp*scale;
order = order+1;
end
tmp = round(tmp*100)/100;
x(2) = tmp * inv(scale)^order;
y(2) = tmp * inv(scale)^order;
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %
......
......@@ -6,7 +6,7 @@ function set_axcomp(obj,parameter,varargin)
%
% See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl
% PBToolbox (2019): JJH: j.heckman@donders.ru.nl
if nargin <2 || isempty(parameter); return; end
......@@ -30,47 +30,3 @@ end
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% p=inputParser;
% my_addParameter(p,'scale','fixed'); %options 'free' 'free_x' 'free_y' 'independent'
% my_addParameter(p,'space','fixed'); %'free_x','free_y','free'
% my_addParameter(p,'force_ticks',false);
% my_addParameter(p,'column_labels',true);
% my_addParameter(p,'row_labels',true);
% parse(p,varargin{:});
%
% obj.facet_scale=p.Results.scale;
% obj.facet_space=p.Results.space;
% obj.column_labels=p.Results.column_labels;
% obj.row_labels=p.Results.row_labels;
%
% if strcmp(obj.facet_scale,'independent') %Force ticks by default in that case
% obj.force_ticks=true;
% else
% obj.force_ticks=p.Results.force_ticks;
% end
%
% %Handle case where facet_grid is called after update()
% if obj.updater.updated
% if isnumeric(obj.aes.row) && isnumeric(obj.aes.column) && all(obj.aes.row==1) && all(obj.aes.column==1)
% if isempty(obj.aes.row) && isempty(obj.aes.column)
% %User probably tried to update all the data
% obj.updater.facet_updated=0;
% else
% %We go from one to multiple facets
% obj.updater.facet_updated=1;
% end
% else
% if isempty(row) && isempty(col)
% %We go from multiple to one facet
% obj.updater.facet_updated=-1;
% else
% error('Updating facet only works when going from one to multiple facets or vice versa');
% end
% end
% end
%
% obj.aes.row=shiftdim(row);
% obj.aes.column=shiftdim(col);
%
% obj.wrap_ncols=-1;
% end
\ No newline at end of file
function set_grid(obj,varargin)
% PB_DRAFT>SET_GRID
%
% SET_TITLE(obj,title,varargin) ...
% SET_GRID(obj,title,varargin) will set grid parameters.
%
% See also ...
% See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl
% PBToolbox (2019): JJH: j.heckman@donders.ru.nl
v = varargin;
f.Box = pb_keyval('box',v,'off');
f.TickDir = pb_keyval('tickdir',v,'in');
f.TickLength = pb_keyval('ticklength',v,[.02 .02]);
f.XMinorTick = pb_keyval('XMinorTick',v,'on');
f.YMinorTick = pb_keyval('YMinorTick',v,'on');
f.YGrid = pb_keyval('YGrid',v,'on');
f.XGrid = pb_keyval('XGrid',v,'on');
f.YColor = pb_keyval('YColor',v,[.3 .3 .3]);
f.XColor = pb_keyval('XColor',v,[.3 .3 .3]);
f.FontSize = 10;
f.YDir = 'normal';
f.LineWidth = .1;
obj(1).grid.bool = true;
obj(1).grid.features = f;
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %
% Part of Programmeer Beer Toolbox (PBToolbox) %
% Written by: Jesse J. Heckman (2018) %
% Written by: Jesse J. Heckman (2019) %
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
%% Example code 'draft'
pb_clean;
load('exD_saccade');
%% first figure draft
cfn = pb_newfig(0,'size',[0 0 17 17],'resize','off');
for i = 1:2
for j = 1:2
d(j,i) = pb_draft('x',Saccades.GazeLatency,'y',Saccades.HeadLatency,'color',Saccades.Modality,'subtitle',['s00' num2str(j)]);
d(j,i).set_labels('x','Gaze Latency (ms)','y','Head Latency (ms)');
if ~iseven(j)
d(j,i).plot_rawdata;
else
d(j,i).plot_bubble;
d(j,i).fit_ellipse;
%% Initialize
% Prepare example_draft: clean and initialize.
pb_clean; % Empty
load('exD_saccade'); % load example data
cfn = 0; % Initialize current figure number
run = 2; % Set # of blocks to run
%% Block1:
% Make figure 1: Plot rawdata + bubbleplot
if run>cfn
cfn = pb_newfig(cfn,'size',[0 0 17 12],'resize','off');
for i = 1:1
for j = 1:2
d(j,i) = pb_draft('x',Saccades.GazeLatency,'y',Saccades.HeadLatency,'color',Saccades.Modality);
d(j,i).set_labels('x','Gaze Latency (ms)','y','Head Latency (ms)');
if ~iseven(i*j)
d(j,i).plot_rawdata;
else
d(j,i).plot_bubble;
d(j,i).fit_ellipse;
end
d(j,i).plot_dline;
d(j,i).plot_vline('type','mode');
d(j,i).plot_hline('type','mode');
end
%d(j,i).fit_ellipse;
d(j,i).plot_dline;
d(j,i).plot_vline('type','mode');
d(j,i).plot_hline('type','mode');
end
d.set_title('Saccade Latencies');
d.set_axcomp(Saccades.Subject);
d.set_grid;
d.draft;
d.print('disp',true);
end
d.set_title('Saccade Latencies');
d.draft;
%d.print('disp',true);
%% Block2:
% Make figure 2: Plot rawdata + bubbleplot
if run>cfn
cfn = pb_newfig(cfn,'size',[0 0 17 17],'resize','off');
d = pb_draft('x',Saccades.GazeLatency,'y',Saccades.HeadLatency,'color',Saccades.Modality);
end
%% second figure draft
cfn = pb_newfig(cfn,'size',[0 0 17 17],'resize','off');
%% Block3:
d = pb_draft('x',Saccades.GazeLatency,'y',Saccades.HeadLatency,'color',Saccades.Modality);
d.set_axcomp([1:2]);
if run>cfn
cfn = pb_newfig(cfn,'size',[0 0 17 17],'resize','off');
d = pb_draft('x',Saccades.GazeLatency,'y',Saccades.HeadLatency,'color',Saccades.Modality);
end
\ No newline at end of file
......@@ -17,22 +17,14 @@ function h = pb_dline(varargin)
style = pb_keyval('style',varargin,'k--');
visibility = pb_keyval('visibility',varargin,'off');
position = pb_keyval('position',varargin);
lim = pb_keyval('lim',varargin,[get(gca,'XLim') get(gca,'YLim')]);
ho = ishold;
if ~ho; hold on; end
%% Obtain axis for empty plot
if isempty(position)
axxes = axis;
min_x = min(axxes([1 3])); axxes([1 3]) = min_x;
max_x = max(axxes([2 4])); axxes([2 4]) = max_x;
position = [min_x, max_x];
end
%% Plot diagonal
h = plot([position(1) position(2)],[position(1) position(2)],style);
h = plot([min(lim) max(lim)],[min(lim) max(lim)],style);
set(h,'Tag','graphical aid: diagonal');
set(h,'HandleVisibility',visibility); % set handle visibility
......
......@@ -19,6 +19,7 @@ function h = pb_hline(y, varargin)
style = pb_keyval('style',varargin,'k--');
visibility = pb_keyval('visibility',varargin,'off');
lim = pb_keyval('lim',varargin,get(gca,'XLim'));