Skip to content

Commit

Permalink
Merge pull request #47 from UoMResearchIT/earlier-scripts
Browse files Browse the repository at this point in the history
Earlier scripts
  • Loading branch information
fherreazcue authored Dec 8, 2023
2 parents e97654d + 254c768 commit bfa27c6
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 222 deletions.
2 changes: 1 addition & 1 deletion code/02-arrays.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
% ## Initializing an Array
Z = zeros(3,5)
Z = zeros(3)
Z = zeros(1x5)
Z = zeros(1,5)
R = rand(8);
O = ones(10,10);

Expand Down
5 changes: 5 additions & 0 deletions code/03-loading_data.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
% ## Most inflamed patients
% !! Solution:
find(per_patient_max == global_max)
% !! Solution:
% ## Alternative solution
find(patient_data == global_max)
[r,c]=ind2sub(size(patient_data),find(patient_data == global_max))




Expand Down
74 changes: 41 additions & 33 deletions code/04-plotting.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@



title('Daily average inflammation')
xlabel('Day of trial')
ylabel('Inflammation')
title("Daily average inflammation")
xlabel("Day of trial")
ylabel("Inflammation")
plot(per_day_max)
title('Maximum inflammation per day')
ylabel('Inflammation')
xlabel('Day of trial')
title("Maximum inflammation per day")
ylabel("Inflammation")
xlabel("Day of trial")

% ## Scripts
edit src/single_plot.m
addpath("src")

plot(per_day_min)
title('Minimum inflammation per day')
ylabel('Inflammation')
xlabel('Day of trial')
title("Minimum inflammation per day")
ylabel("Inflammation")
xlabel("Day of trial")
% ## Multiple lines in a plot
copyfile("src/single_plot.m","src/multiline_plot.m")
plot(per_day_mean,DisplayName="Mean")
legend
title('Daily average inflammation')
xlabel('Day of trial')
ylabel('Inflammation')
title("Daily average inflammation")
xlabel("Day of trial")
ylabel("Inflammation")
hold on
plot(patient_5,DisplayName="Patient 5")
hold off
Expand All @@ -34,37 +40,38 @@
% !! Solution:
plot(per_day_mean,DisplayName="Mean")
legend
title('Daily average inflammation')
xlabel('Day of trial')
ylabel('Inflammation')
title("Daily average inflammation")
xlabel("Day of trial")
ylabel("Inflammation")
hold on
plot(patient_data(3,:),DisplayName="Patient 3")
plot(patient_data(4,:),DisplayName="Patient 4")
hold off


% ## Multiple plots in a figure
edit src/tiled_plot.m
tiledlayout(1, 2)
nexttile
plot(per_day_max)
title('Max')
xlabel('Day of trial')
ylabel('Inflamation')
title("Max")
xlabel("Day of trial")
ylabel("Inflamation")
nexttile
plot(per_day_min)
title('Min')
xlabel('Day of trial')
ylabel('Inflamation')
tlo=tiledlayout(1, 2)
title(tlo,'Per day data')
xlabel(tlo,'Day of trial')
ylabel(tlo,'Inflamation')
title("Min")
xlabel("Day of trial")
ylabel("Inflamation")
tlo=tiledlayout(1, 2);
title(tlo,"Per day data")
xlabel(tlo,"Day of trial")
ylabel(tlo,"Inflamation")
nexttile
plot(per_day_max)
title('Max')
title("Max")
nexttile
plot(per_day_min)
title('Min')
title("Min")

% ## Where is the `nexttile`?
tiledlayout(3,5)
Expand All @@ -78,15 +85,16 @@


% ## Clearing a figure
clf

% ## Heatmaps
heatmap(patient_data)
title('Inflammation')
xlabel('Day of trial')
ylabel('Patient number')
title("Inflammation")
xlabel("Day of trial")
ylabel("Patient number")
imagesc(patient_data)
title('Inflammation')
xlabel('Day of trial')
ylabel('Patient number')
title("Inflammation")
xlabel("Day of trial")
ylabel("Patient number")


86 changes: 38 additions & 48 deletions code/05-scripts.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
% ## The MATLAB path

% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');
patient_data = readmatrix("data/base/inflammation-01.csv");

% Compute global statistics
g_mean = mean(patient_data(:));
Expand All @@ -20,12 +20,12 @@
p_min = min(patient_data(5,:));

% Compare patient vs global
disp('Patient 5:')
disp('High mean?')
disp("Patient 5:")
disp("High mean?")
disp(p_mean > g_mean)
disp('Highest max?')
disp("Highest max?")
disp(p_max == g_max)
disp('Lowest min?')
disp("Lowest min?")
disp(p_min == g_min)

% ## Comments
Expand All @@ -34,28 +34,27 @@
clc
patient_analysis
% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');
patient_data = readmatrix("data/base/inflammation-01.csv");

% Compute global statistics
g_mean = mean(patient_data(:));
g_max = max(patient_data(:));
g_min = min(patient_data(:));

patient_number = 8;

% Compute patient statistics
patient_number = 8;
p_mean = mean(patient_data(patient_number,:));
p_max = max(patient_data(patient_number,:));
p_min = min(patient_data(patient_number,:));

% Compare patient vs global
disp('Patient:')
disp("Patient:")
disp(patient_number)
disp('High mean?')
disp("High mean?")
disp(p_mean > g_mean)
disp('Highest max?')
disp("Highest max?")
disp(p_max == g_max)
disp('Lowest min?')
disp("Lowest min?")
disp(p_min == g_min)
patient_analysis

Expand All @@ -65,83 +64,74 @@
help patient_analysis
% ## Script for plotting
edit src/plot_daily_average.m
% PLOT_DAILY_AVERAGE Plots daily average inflammation accross patients.

% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');

figure

% Plot average inflammation per day
plot(mean(patient_data, 1))
title('Daily average inflammation')
xlabel('Day of trial')
ylabel('Inflammation')
figure
plot_daily_average
% ### Modified script for sub-plots
% PLOT_DAILY_AVERAGE Plots daily average, max and min inflammation accross patients.

% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');
patient_data = readmatrix("data/base/inflammation-01.csv");

figure
fig = figure;

% Define tiled layout and labels
tlo = tiledlayout(1,3);
xlabel(tlo,'Day of trial')
ylabel(tlo,'Inflammation')
xlabel(tlo,"Day of trial")
ylabel(tlo,"Inflammation")

% Plot average inflammation per day
nexttile
plot(mean(patient_data, 1))
title('Average')
title("Average")

% Plot max inflammation per day
nexttile
plot(max(patient_data, [], 1))
title('Max')
title("Max")

% Plot min inflammation per day
nexttile
plot(min(patient_data, [], 1))
title('Min')
% Save plot in 'results' folder as png image:
print('results/daily_average_01','-dpng')
title("Min")
figure
plot_daily_average
% ### Saving figures
% Save plot in "results" folder as png image:
saveas(fig,"results/daily_average_01.png")

% Save plot in 'results' folder as png image:
saveas(gcf,'results/daily_average_01.png')
% ## Getting the current figure
% Save plot in "results" folder as png image:
saveas(gcf,"results/daily_average_01.png")
gcf == fig

% ### Hiding figures
% PLOT_DAILY_AVERAGE Saves plot of daily average, max and min inflammation accross patients.

% Load patient data
patient_data = readmatrix('data/base/inflammation-01.csv');
patient_data = readmatrix("data/base/inflammation-01.csv");

figure(visible='off')
fig = figure(Visible='off');

% Define tiled layout and labels
tlo = tiledlayout(1,3);
xlabel(tlo,'Day of trial')
ylabel(tlo,'Inflammation')
xlabel(tlo,"Day of trial")
ylabel(tlo,"Inflammation")

% Plot average inflammation per day
nexttile
plot(mean(patient_data, 1))
title('Average')
title("Average")

% Plot max inflammation per day
nexttile
plot(max(patient_data, [], 1))
title('Max')
title("Max")

% Plot min inflammation per day
nexttile
plot(min(patient_data, [], 1))
title('Min')
title("Min")

% Save plot in 'results' folder as png image:
saveas(gcf,'results/daily_average_01.png')
% Save plot in "results" folder as png image:
saveas(fig,"results/daily_average_01.png")

close()
close(fig)


4 changes: 2 additions & 2 deletions episodes/02-arrays.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Arrays
teaching: 30
exercises: 30
exercises: 10
---

::::::::::::::::::::::::::::::::::::::: objectives
Expand Down Expand Up @@ -47,7 +47,7 @@ Z =
yields a 3×3 array.
If we want a single row and 5 columns, we need to remember that MATLAB reads `rows`×`columns`, so
```matlab
>> Z = zeros(1x5)
>> Z = zeros(1,5)
```
```output
Z =
Expand Down
38 changes: 37 additions & 1 deletion episodes/03-loading_data.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Loading data
teaching: 35
teaching: 25
exercises: 15
---

Expand Down Expand Up @@ -453,6 +453,42 @@ ans =
```
So patient 31 has the maximum inflamation level.

<br>

:::::::::::: solution

## Alternative solution

We can only do this because we had already calculated `per_patient_max`.
However, there is another way of doing this.
Just as we used `find` to locate the patients that had a maximum inflammation value equal to the global maximum,
we can find the value from the whole data set:
```matlab
>> find(patient_data == global_max)
```
```
ans =
391
```
However, this resulted in a rather odd number.
This number represents the linear index of the global maximum.
Linear indices result from counting through the elements in the first column, then continue the count on the second column and so on.
Luckily, there is a function to convert this linear index into a row and number column, `ind2sub`.
We need to provide the size of our array, and the linear index, i.e. `ind2sub([60,40],391)`.
We also need to provide space for both outputs (the row and column numbers), so we call it as `[r,c]=ind2sub([60,40],391)`.
ternatively, we can get the size and index inside the call:
```matlab
>> [r,c]=ind2sub(size(patient_data),find(patient_data == global_max))
```
```
r =
31
c =
7
```

::::::::::::

:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::
Expand Down
Loading

0 comments on commit bfa27c6

Please sign in to comment.