Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions AMF/+AMF/+utils/parfor_progress.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function percent = parfor_progress(N)
%PARFOR_PROGRESS Progress monitor (progress bar) that works with parfor.
% PARFOR_PROGRESS works by creating a file called parfor_progress.txt in
% your working directory, and then keeping track of the parfor loop's
% progress within that file. This workaround is necessary because parfor
% workers cannot communicate with one another so there is no simple way
% to know which iterations have finished and which haven't.
%
% PARFOR_PROGRESS(N) initializes the progress monitor for a set of N
% upcoming calculations.
%
% PARFOR_PROGRESS updates the progress inside your parfor loop and
% displays an updated progress bar.
%
% PARFOR_PROGRESS(0) deletes parfor_progress.txt and finalizes progress
% bar.
%
% To suppress output from any of these functions, just ask for a return
% variable from the function calls, like PERCENT = PARFOR_PROGRESS which
% returns the percentage of completion.
%
% Example:
%
% N = 100;
% parfor_progress(N);
% parfor i=1:N
% pause(rand); % Replace with real code
% parfor_progress;
% end
% parfor_progress(0);
%
% See also PARFOR.

% By Jeremy Scheff - jdscheff@gmail.com - http://www.jeremyscheff.com/

% error(nargchk(0, 1, nargin, 'struct'));

if nargin < 1
N = -1;
end

percent = 0;
w = 50; % Width of progress bar

if N > 0
f = fopen('parfor_progress.txt', 'w');
if f<0
error('Do you have write permissions for %s?', pwd);
end
fprintf(f, '%d\n', N); % Save N at the top of progress.txt
fclose(f);

if nargout == 0
disp([' 0%[>', repmat(' ', 1, w), ']']);
end
elseif N == 0
delete('parfor_progress.txt');
percent = 100;

if nargout == 0
disp([repmat(char(8), 1, (w+9)), char(10), '100%[', repmat('=', 1, w+1), ']']);
end
else

if ~exist('parfor_progress.txt', 'file')
error('parfor_progress.txt not found. Run PARFOR_PROGRESS(N) before PARFOR_PROGRESS to initialize parfor_progress.txt.');
end

f = fopen('parfor_progress.txt', 'a');
fprintf(f, '1\n');
fclose(f);

f = fopen('parfor_progress.txt', 'r');
progress = fscanf(f, '%d');
fclose(f);
percent = (length(progress)-1)/progress(1)*100;

if nargout == 0
perc = sprintf('%3.0f%%', percent); % 4 characters wide, percentage
disp([repmat(char(8), 1, (w+9)), char(10), perc, '[', repmat('=', 1, round(percent*w/100)), '>', repmat(' ', 1, w - round(percent*w/100)), ']']);
end
end
82 changes: 82 additions & 0 deletions AMF/+AMF/+utils/parfor_progress2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
function percent = parfor_progress2(N)
%PARFOR_PROGRESS Progress monitor (progress bar) that works with parfor.
% PARFOR_PROGRESS works by creating a file called parfor_progress.txt in
% your working directory, and then keeping track of the parfor loop's
% progress within that file. This workaround is necessary because parfor
% workers cannot communicate with one another so there is no simple way
% to know which iterations have finished and which haven't.
%
% PARFOR_PROGRESS(N) initializes the progress monitor for a set of N
% upcoming calculations.
%
% PARFOR_PROGRESS updates the progress inside your parfor loop and
% displays an updated progress bar.
%
% PARFOR_PROGRESS(0) deletes parfor_progress.txt and finalizes progress
% bar.
%
% To suppress output from any of these functions, just ask for a return
% variable from the function calls, like PERCENT = PARFOR_PROGRESS which
% returns the percentage of completion.
%
% Example:
%
% N = 100;
% parfor_progress(N);
% parfor i=1:N
% pause(rand); % Replace with real code
% parfor_progress;
% end
% parfor_progress(0);
%
% See also PARFOR.

% By Jeremy Scheff - jdscheff@gmail.com - http://www.jeremyscheff.com/

% error(nargchk(0, 1, nargin, 'struct'));

if nargin < 1
N = -1;
end

percent = 0;
w = 50; % Width of progress bar

if N > 0
f = fopen('parfor_progress.txt', 'w');
if f<0
error('Do you have write permissions for %s?', pwd);
end
fprintf(f, '%d\n', N); % Save N at the top of progress.txt
fclose(f);

if nargout == 0
disp([' 0%[>', repmat(' ', 1, w), ']']);
end
elseif N == 0
delete('parfor_progress.txt');
percent = 100;

if nargout == 0
disp([repmat(char(8), 1, (w+9)), char(10), '100%[', repmat('=', 1, w+1), ']']);
end
else

if ~exist('parfor_progress.txt', 'file')
error('parfor_progress.txt not found. Run PARFOR_PROGRESS(N) before PARFOR_PROGRESS to initialize parfor_progress.txt.');
end

% f = fopen('parfor_progress.txt', 'a');
% fprintf(f, '1\n');
% fclose(f);

f = fopen('parfor_progress.txt', 'r');
progress = fscanf(f, '%d');
fclose(f);
percent = (length(progress)-1)/progress(1)*100;

if nargout == 0
perc = sprintf('%3.0f%%', percent); % 4 characters wide, percentage
disp([perc, '[', repmat('=', 1, round(percent*w/100)), '>', repmat(' ', 1, w - round(percent*w/100)), ']']);
end
end
28 changes: 21 additions & 7 deletions AMF/+AMF/@ModelResult/plotAll.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
function this = plotAll(this, type, mode)
function this = plotAll(this, type, varargin)

if nargin < 3, mode = 'TRAJ'; end
if nargin < 3, mode = 'TRAJ';
else mode = varargin{1};
end

if length(varargin)>1
split = varargin{2};
else
split = 9;
end

import AMF.utils.defineCustomColormap

Expand All @@ -9,15 +17,20 @@
comps = comps(logical([this.parameters.fit]));
end


n = length(comps);
ns = sqrt(n);
ns = sqrt(split);
% ns = sqrt(n);

numIter = this.options.numIter;

figure('Name', upper(type));

for i = 1:n
subplot(ceil(ns),ceil(ns),i); hold on;
figure(ceil(i/split));
counter = ceil(i/split);
subplot(ceil(ns),ceil(ns),i-(counter-1)*split); hold on;
% subplot(ceil(ns),ceil(ns),i); hold on;

comp = comps(i);

Expand All @@ -31,9 +44,10 @@
plotHist(this, comp, colorMap);

case 'HIST_LOG'

% TODO: plot logarithmic histograms

% TODO: plot logarithmic histograms
case 'MAD' % median absolute deviation
plotMad(this, comp, 'g');

otherwise
error('Unknown plot mode %s', mode);
end
Expand Down
22 changes: 22 additions & 0 deletions AMF/+AMF/@ModelResult/plotMad.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function [hp1, hp2] = plotMad(this, comp, color)
% plot median absolute deviation
time_steps = this.options.numTimeSteps;
meta = zeros(time_steps,4);
for dt = 1:time_steps
y = quantile(comp.val(dt,:),[0.05 0.16 0.25 0.5 0.75 0.84 0.95], 2);
meta(dt,1) = y(4);
meta(dt,2) = median(abs(comp.val(dt,:)- meta(dt,1)));
meta(dt,3) = meta(dt,1)+meta(dt,2);
meta(dt,4) = meta(dt,1)-meta(dt,2);
end
time = 1:time_steps;
time = time*this.time(end)/time_steps;
X = [time fliplr(time)];
Yupp = meta(:,3)';
Ylwr = meta(:,4)';
Y = [Ylwr fliplr(Yupp)];
hp1 = fill(X, Y, color);
hp2 = plot(time, meta(:,1), '-k', 'LineWidth', 2);

end

20 changes: 19 additions & 1 deletion AMF/+AMF/@ModelResult/plot.m → AMF/+AMF/@ModelResult/plots.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function this = plot(this, names, mode)
function this = plots(this, names, mode)

if nargin < 3, mode = 'TRAJ'; end

Expand Down Expand Up @@ -31,6 +31,24 @@
case 'HIST_LOG'

% TODO: plot logarithmic histograms
case 'MAD' % median absolute deviation
time_steps = this.options.numTimeSteps;
meta = zeros(time_steps,4);
for dt = 1:time_steps
y = quantile(comp.val(dt,:),[0.05 0.16 0.25 0.5 0.75 0.84 0.95], 2);
meta(dt,1) = y(4);
meta(dt,2) = median(abs(comp.val(dt,:)- meta(dt,1)));
meta(dt,3) = meta(dt,1)+meta(dt,2);
meta(dt,4) = meta(dt,1)-meta(dt,2);
end
time = 1:time_steps;
time = time*this.time(end)/time_steps;
X = [time fliplr(time)];
Yupp = meta(:,3)';
Ylwr = meta(:,4)';
Y = [Ylwr fliplr(Yupp)];
hp = fill(X, Y, 'g');
plot(time, meta(:,1), '-k', 'LineWidth', 2);

otherwise
error('Unknown plot mode %s', mode);
Expand Down
8 changes: 6 additions & 2 deletions AMF/+AMF/runADAPT.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
result = model.result;

tic
utils.parfor_progress(numIter);
parfor it = 1:numIter
model2 = model; % parfor fix
elt = 0;
Expand All @@ -47,14 +48,17 @@

success = 1;
catch err
%
disp(err.message);
utils.parfor_progress2;
end
end
utils.parfor_progress;

fprintf('Computed trajectory %d [%d] - %.2fs\n', it, max(model2.result.sse), elt);
% fprintf('Computed trajectory %d [%d] - %.2fs\n', it, max(model2.result.sse), elt);

result(it) = model2.result;
end
parfor_progress(0);
toc

result = AMF.ModelResult(model, result);
Expand Down
Binary file added AMF/models/dietModel/Hall/BW.emf
Binary file not shown.
Binary file added AMF/models/dietModel/Hall/Hall.emf
Binary file not shown.
9 changes: 9 additions & 0 deletions AMF/models/dietModel/Hall/Hall.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function EE = Hall(t, dEI )
%Energy Expenditure in mice according to Hall
LM = 25;
FM = -0.0649.*t.^2 + 2.0961.*t + 2.5263;

EE = 2.1 + 0.331.*LM + 0.202.*FM + 0.4.*dEI;

end

Binary file added AMF/models/dietModel/Hall/Hall1.emf
Binary file not shown.
21 changes: 21 additions & 0 deletions AMF/models/dietModel/Hall/bw_fit.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
% Fit body weight data

time = diets.t3/(24*7); % weeks HFD
TG = diets.perTG_m*853/1e6; % g TG

% fit to polynomial
p = polyfit(time, TG, 2); % -0.0649*x^2 + 2.0961*x + 2.5263

% plot polynomial and data
x1 = linspace(0, 12, 100);
y1 = polyval(p,x1);

figure(1);hold on;
plot(time, TG, 'ok', x1, y1, '-k', 'LineWidth', 2);
xlabel('Time on HFD (weeks)');
ylabel('Body TG (g)');
saveas(1, 'BW.emf');

% slope at t= 3 = 1.7067 g TG/week
% slope at t=10 = 0.7981 g TG/week

11 changes: 11 additions & 0 deletions AMF/models/dietModel/Hall/hall_plot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
% Hall
t = linspace(0,16,100);
t1 = [3 10];
x1 = [11.3+1.35 12.9+1.35];
ee3 = Hall(t, 3);
ee2 = Hall(t, 2);
ee4 = Hall(t, 4);
ee909 = Hall(t, 0.909);
plot(t,ee2, '--k', t, ee3, '-k', t, ee4, '--k',t, ee909, '--b', t1, x1, 'or', 'LineWidth', 2);
xlabel('Time on HFD (weeks)')
ylabel('Energy Expenditure (kcal/day)')
Loading