-
Notifications
You must be signed in to change notification settings - Fork 0
/
pupil_analysis.m
171 lines (120 loc) · 5.93 KB
/
pupil_analysis.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
% How is this analysis done?
% 1. Get the distribution of pupil diameter for all VT trials in stationary
% and motion period (separated)
% 2. calculate the pupil diameter threshold which retains a percentile of
% stationary period data
% 3. on a trial by trial basis, calculate a pupil mask such that the pupil
% diameter is lower than the above threshold.
% 4. we combine the pupil diameter mask with the motion mask (double mask) and we keep
% data under these two.
% 5. we apply the double mask to the V traces
% 6. we compare the firing rates with the same double mask (mean across
% trial per cluser, median over trials per cluster)
experiment_groups = 'visual_flow';
trial_types = {{'VT_RVT', 'VT_RV'}, {'V_RVT', 'V_RV'}};
percentile = 70;
ctl = RC2Analysis();
probe_ids = ctl.get_probe_ids(experiment_groups);
median_pd_V = [];
median_pd_VT = [];
direction = [];
for probe_i = 1 : length(probe_ids)
data = ctl.load_formatted_data(probe_ids{probe_i});
clusters = data.VISp_clusters();
% =====================================================================
% analyse VT
type_i = 1;
% Get the distribution of pupil diameter in stationary and motion periods
trials = data.get_trials_with_trial_group_label(trial_types{type_i});
pupil_diameter_motion_all = zeros(length(trials), 200000);
pupil_diameter_motion_all_stationary = zeros(length(trials), 200000);
for trial_i = 1 : length(trials)
trial = trials{trial_i}.to_aligned;
original_trial = trial.original_trial;
original_motion_mask = original_trial.motion_mask;
original_stationary_mask = original_trial.stationary_mask;
pupil_diameter = trial.pupil_diameter;
pupil_diameter_masked = pupil_diameter(original_motion_mask);
pupil_diameter_masked_stationary = pupil_diameter(original_stationary_mask);
pupil_diameter_motion_all(trial_i, 1:length(pupil_diameter_masked)) = pupil_diameter_masked;
pupil_diameter_motion_all_stationary(trial_i, 1:length(pupil_diameter_masked_stationary)) = pupil_diameter_masked_stationary;
% distribution per trial plot
% figure(trial_i);
% hold on;
% histogram(cam_motion_original_stationary);
% histogram(cam_motion_original_motion);
end
% Set the threshold
pupil_diameter_motion_all(pupil_diameter_motion_all==0) = NaN;
pupil_diameter_motion_all_stationary(pupil_diameter_motion_all_stationary==0) = NaN;
small_diameter_threshold = prctile(pupil_diameter_motion_all_stationary(:), percentile);
figure(probe_i);
hold on;
h = histogram(pupil_diameter_motion_all(:), 50);
hold on;
g = histogram(pupil_diameter_motion_all_stationary(:), 50);
xline(small_diameter_threshold)
% Save the windows in a variables to be reused to analyse V
windows_pd = zeros(length(trials), 350000);
mean_spikes_VT = zeros(length(trials), length(clusters));
total_motion_mask_len = 0;
total_double_mask_len = 0;
for trial_i = 1 : length(trials)
trial = trials{trial_i}.to_aligned;
original_trial = trial.original_trial;
original_motion_mask = original_trial.motion_mask;
pupil_diameter = trial.pupil_diameter;
pd_mask = pupil_diameter < small_diameter_threshold;
pd_doubled_masking = pd_mask & original_motion_mask(1:length(pd_mask));
motion_mask = original_motion_mask(1:length(pd_mask));
windows_pd(trial_i, 1:length(pd_mask)) = pd_doubled_masking;
total_motion_mask_len = total_motion_mask_len + sum(motion_mask(:) == 1);
total_double_mask_len = total_double_mask_len + length(pupil_diameter(pd_doubled_masking));
% To see masks for each trial, uncomment this section
% figure(trial_i);
% hold on;
% plot(pupil_diameter/70, 'g');
% plot(pd_mask * 1, 'k');
% plot(original_motion_mask * 2, 'r');
% plot(pd_doubled_masking, 'y');
% plot(original_trial.stationary_mask * 2, 'b')
for clust_i = 1 : length(clusters)
fr = clusters(clust_i).fr.get_convolution(trial.probe_t);
mean_spikes_VT(trial_i, clust_i) = nanmean(fr(pd_doubled_masking));
end
end
total_double_mask_time = total_double_mask_len / 10e3;
total_motion_mask_time = total_motion_mask_len / 10e3;
sprintf('Probe %s; time retained with two masks: %f, total time: %f, threshold: %f', ...
probe_ids{probe_i}, total_double_mask_time, total_motion_mask_time, small_diameter_threshold)
% =====================================================================
% Analyse V
type_i = 2;
trials = data.get_trials_with_trial_group_label(trial_types{type_i});
mean_spikes_V = zeros(length(trials), length(clusters));
for trial_i = 1 : length(trials)
trial = trials{trial_i}.to_aligned;
for clust_i = 1 : length(clusters)
fr = clusters(clust_i).fr.get_convolution(trial.probe_t);
mean_spikes_V(trial_i, clust_i) = nanmean(fr(logical(windows_pd(trial_i, 1:length(fr)))));
end
end
for clust_i = 1 : length(clusters)
pd_V = mean_spikes_V(:, clust_i);
pd_VT = mean_spikes_VT(:, clust_i);
median_pd_V(end+1) = nanmedian(pd_V);
median_pd_VT(end+1) = nanmedian(pd_VT);
[~, ~, ~, direction(end+1)] = compare_groups_with_signrank(pd_V, pd_VT);
end
end
figure(5);
h_ax = subplot(1, 1, 1);
hold on;
fmt.xy_limits = [0, 60];
fmt.tick_space = 20;
fmt.line_order = 'top';
fmt.xlabel = trial_types{2};
fmt.ylabel = trial_types{1};
fmt.include_inset = false;
fmt.colour_by = 'significance';
unity_plot_plot(h_ax, median_pd_V, median_pd_VT, direction, fmt);