-
Notifications
You must be signed in to change notification settings - Fork 0
/
F_sameOL_diversity.m
221 lines (158 loc) · 7.22 KB
/
F_sameOL_diversity.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
% create a unique id to a pair of chekclit
T2_diff.checklist_id_01 = T2_diff.checklist_id_0 +"_"+ T2_diff.checklist_id_1;
% Load the species level data
load("data/eBird/Tr.mat")
T = vertcat(Tr{:});
Ts = T(:,["species_code","obs_count","checklist_id"]);
Ts = Ts(~isnan(Ts.obs_count),:);
% Filter T to only keep checklist exisit in T2_diff
[id, tmp]= ismember(Ts.checklist_id,T2_diff.checklist_id_0);
Ts0 = Ts(id,:);
Ts0.checklist_id_01 = T2_diff.checklist_id_01(tmp(tmp>0));
[id, tmp]= ismember(Ts.checklist_id,T2_diff.checklist_id_1);
Ts1 = Ts(id,:);
Ts1.checklist_id_01 = T2_diff.checklist_id_01(tmp(tmp>0));
% Join the two daily difference
Ts01 = outerjoin( Ts0, Ts1, "Keys", ["species_code","checklist_id_01"],"MergeKeys",1,"LeftVariables",["obs_count","species_code","checklist_id_01"],"RightVariables",["obs_count"]);
% set nan (no obs) to zero
Ts01.obs_count_Ts0(isnan(Ts01.obs_count_Ts0))=0;
Ts01.obs_count_Ts1(isnan(Ts01.obs_count_Ts1))=0;
% Compute number of individual gained and lost
Ts01.ind_gain = Ts01.obs_count_Ts1-Ts01.obs_count_Ts0;
Ts01.ind_gain(Ts01.ind_gain<0) = 0;
Ts01.ind_lost = Ts01.obs_count_Ts0-Ts01.obs_count_Ts1;
Ts01.ind_lost(Ts01.ind_lost<0) = 0;
% COmpute number of species gained and lost
Ts01.sp_gain = Ts01.obs_count_Ts1>0 & Ts01.obs_count_Ts0==0;
Ts01.sp_lost = Ts01.obs_count_Ts1==0 & Ts01.obs_count_Ts0>0;
% Combine all species
Ts01s = groupsummary(Ts01,"checklist_id_01","sum",["ind_gain", "ind_lost", "sp_gain", "sp_lost"]);
% Join with T2_diff
T2_diffc = outerjoin( T2_diff, Ts01s, "Keys", "checklist_id_01", "MergeKeys",1);
T2_diffc.sum_sp_gain(isnan(T2_diffc.sum_sp_gain)) = 0;
T2_diffc.sum_sp_lost(isnan(T2_diffc.sum_sp_lost)) = 0;
T2_diffc.sum_ind_gain(isnan(T2_diffc.sum_ind_gain)) = 0;
T2_diffc.sum_ind_lost(isnan(T2_diffc.sum_ind_lost)) = 0;
T2_diffc.GroupCount(isnan(T2_diffc.GroupCount)) = 0;
%% Combine data at the daily scale
% filter
T2_diffcf = T2_diffc;
T2_diffcf = T2_diffcf(T2_diffcf.cci>-Inf,:);
% T2_diffcf = T2_diffcf(abs(T2_diffcf.ddist)<3,:);
% T2_diffcf = T2_diffcf(abs(T2_diffcf.dhrs)<Inf,:);
% T2_diffcf = T2_diffcf(abs(T2_diffcf.dhours_since_sunrise)<Inf,:);
% group per day (and location)
[G,TID]=findgroups(T2_diffcf(:,["ts","name"]));
% number of species
TID.sp_chg_m=splitapply(@median,T2_diffcf.sum_sp_lost+T2_diffcf.sum_sp_gain,G);
TID.sp_chg_q25=splitapply(@(x) quantile(x,.25),T2_diffcf.sum_sp_lost+T2_diffcf.sum_sp_gain,G);
TID.sp_chg_q75=splitapply(@(x) quantile(x,.75),T2_diffcf.sum_sp_lost+T2_diffcf.sum_sp_gain,G);
%number of individual
TID.ind_chg_m=splitapply(@median,T2_diffcf.sum_ind_lost+T2_diffcf.sum_ind_gain,G);
TID.ind_chg_q25=splitapply(@(x) quantile(x,.25),T2_diffcf.sum_ind_lost+T2_diffcf.sum_ind_gain,G);
TID.ind_chg_q75=splitapply(@(x) quantile(x,.75),T2_diffcf.sum_ind_lost+T2_diffcf.sum_ind_gain,G);
% number of checklist
TID.nb=splitapply(@numel,T2_diffcf.sum_ind_lost+T2_diffcf.sum_ind_gain,G);
% combine with weather radar data
Tk = outerjoin(TID,Trd,MergeKeys=true,RightKeys=["name","obs_dt_day"],LeftKeys=["name","ts"]);
Tk = renamevars(Tk,"ts_obs_dt_day","ts");
% Compute correlation
% filter rain and number per day
thr_nb = 10;
thr_rain = 0; % 5e-6;
c=nan(height(cities),2);
for i_c=1:height(cities)
Tkt = Tk(Tk.name==cities.name(i_c) & Tk.nb>=thr_nb,:);
if height(Tkt)>0
c(i_c,1)=corrW(Tkt.takingoff-Tkt.landing,Tkt.ind_chg_m,Tkt.nb);
% c(i_c,1)=corrW(Tkt.mvt,Tkt.ind_chg_m,Tkt.nb);
c(i_c,2) = height(Tkt);
end
end
figure('position',[0 0 800 350]); tiledlayout('flow','TileSpacing','tight','Padding','tight');
hold on; box on; grid on;
cl = crameri("batlow");
scatter(c(:,1),1:height(cities),c(:,2),'o', "filled", Color=cl(end/2+20,:))
xlim([0 .6])
set(gca,"ydir","reverse")
ylim([0 height(cities)+1])
yticks(1:height(cities))
yticklabels(cities.name)
%legend('Random combinaison','Optimal combinaison','True combinaison',TextColor="w",FontSize=16);
xlabel('Correlation')
%exportgraphics(gcf, "figures/corr_all_spring.png")
%exportgraphics(gcf, "figures_paper/figure_6.png",'BackgroundColor','k')
xline(sum(c(:,1).*c(:,2))/sum(c(:,2)))
nanmean(nansum(c(:,1).*c(:,2))/nansum(c(:,2)))
%%
i_c=3;
figure('position',[0 0 900 750]); tiledlayout(4, 2,'TileSpacing','tight','Padding','tight');
y_wr_max = 200;
for i_y=2021:-1:2017
Tkt = Tk(year(Tk.ts)==i_y&Tk.name==cities.name(i_c),:);
if i_y==2021
nexttile([2,2]);
else
nexttile;
end
hold on; box on; grid on;
% bar(Tkt.ts,-Tkt.Fd_takingoff(:,i_c)-Fd_landing(:,i_c),1,'FaceColor','w');
%tmp = cumsum(-Tkt.takingoff-Tkt.landing,'omitnan');
%tmp = tmp - mean(tmp) + y_wr_max/2;
bar(Tkt.ts,Tkt.takingoff,1,'FaceColor',clmap(end,:),FaceAlpha=1,EdgeColor=[.5 .5 .5])
bar(Tkt.ts,-Tkt.landing,1,'FaceColor',clmap(1,:),FaceAlpha=1,EdgeColor=[.5 .5 .5])
bar(Tkt.ts,min(Tkt.takingoff,-Tkt.landing),1,FaceAlpha=1,FaceColor=[.85 .85 .85],EdgeColor=[.5 .5 .5])
% plot(Tkt.ts+.5,tmp,'--k','LineWidth',3)
% scatter(Tkt.ts(Tkt.rain>5e-6), max(Tkt.takingoff(Tkt.rain>5e-6),-Tkt.landing(Tkt.rain>5e-6)), 50,"k",'filled')
% scatter(Tkt.ts(Tkt.rain>5e-6), max(Tkt.takingoff(Tkt.rain>5e-6),-Tkt.landing(Tkt.rain>5e-6)), 20,"k",'filled')
%ylim([0 y_wr_max])
ylim([0 y_wr_max]);y_lim=ylim;
yticks(y_lim(1):(y_lim(2)/5):y_lim(2))
xlim([ datetime(i_y,4,1) datetime(i_y,6,1)])
text(datetime(i_y,4,1), y_lim(2)," "+num2str(i_y),HorizontalAlignment="left",VerticalAlignment="top",FontSize=24,FontWeight="bold",Color=[.2 .2 .2])
datetick('x','dd-mmm',"keeplimits")
if i_y ~= 2021
xticklabels(''); yticklabels('');
else
ylabel("Weather radar nightly change (bird/km^2)")
end
set(gca,'XColor','k'); set(gca,'yColor','k');
yyaxis right
errorbar(Tkt.ts, Tkt.ind_chg_m, Tkt.ind_chg_m-Tkt.ind_chg_q25, Tkt.ind_chg_q75-Tkt.ind_chg_m,"color",cleBird,"LineStyle","none")
scatter(Tkt.ts, Tkt.ind_chg_m,Tkt.nb*5,'filled',Color=cleBird)
ylim([0 40])
end
% exportgraphics(gcf, "figures_paper/figure_5.png",'BackgroundColor','k')
%%
%% Combine with radar
Tq = join(T2_diffc,Trd,RightKeys=["name","obs_dt_day"],LeftKeys=["name","ts"]);
Tqf = Tq;
%Tqf = Tqf(Tqf.cci>-2,:);
% Tqf = Tqf(abs(Tqf.ddist)<1,:);
%Tqf = Tqf(abs(Tqf.dhrs)<Inf,:);
%Tqf = Tqf(abs(Tqf.rain)<=0,:);
%Tqf = Tqf(abs(Tqf.dhours_since_sunrise)<Inf,:);
c=nan(height(cities),2);
for i_c=1:height(cities)
Tqt = Tqf(Tqf.name==cities.name(i_c),:);
if height(Tqt)>0
c(i_c,1)=corr(Tqt.takingoff-Tqt.landing,Tqt.sum_sp_gain+Tqt.sum_sp_lost);
% corr(Tq.takingoff-Tq.landing,Tq.sum_sp_lost+Tq.sum_sp_gain,"rows","complete");
%c(i_c,1)=corr(Tkt.takingoff-Tkt.landing,Tkt.ind_chg_m,"rows","complete");
c(i_c,2) = height(Tqt);
end
end
figure('position',[0 0 900 450]); tiledlayout('flow','TileSpacing','tight','Padding','tight');
hold on; box on; grid on;
% scatter(1:height(cities),c(:,1),c(:,2),'o',Color=cl(end/2+20,:), linewidth=2)
plot(1:height(cities),c(:,1),'o')
ylim([-.1 .7])
xlim([0 height(cities)])
xticks(1:height(cities))
xticklabels(cities.name)
%legend('Random combinaison','Optimal combinaison','True combinaison',TextColor="w",FontSize=16);
ylabel('Correlation')
%exportgraphics(gcf, "figures/corr_all_spring.png")
%exportgraphics(gcf, "figures_paper/figure_6.png",'BackgroundColor','k')
yline(nanmean(c(:,1)))
nanmean(c(:,1))