-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutility.r
1249 lines (1076 loc) · 49.9 KB
/
utility.r
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# useful function
# give it an object and it will return the biomass data ready to be plotted (for species and phenotypes)
biom <-function(object,phenotype=T)
{
biomass <- getBiomass(object) # n * w * dw and sum by species
SpIdx = NULL # getting rid of the species that went extinct at the beginning
for (i in unique(object@params@species_params$species))
if (sum(biomass[,i]) != 0 & dim(object@params@species_params[object@params@species_params$species == i,])[1] != 1)
SpIdx = c(SpIdx,i)
biomassTot = NULL
biomassTemp = biomass
colnames(biomassTemp) = object@params@species_params$species
for (i in SpIdx)
{
biomassSp = biomassTemp[,which(colnames(biomassTemp) == i)]
biomassSp = apply(biomassSp,1,sum)
biomassTot = cbind(biomassTot,biomassSp)
}
colnames(biomassTot) = SpIdx
# biomassTot is the biomass of species
# biomass is the biomass of phenotypes
plotB <- function(x)
{
Biom <- melt(x) # melt for ggplot
names(Biom) = list("time","sp","value")
# Due to log10, need to set a minimum value, seems like a feature in ggplot
min_value <- 1e-300
Biom <- Biom[Biom$value >= min_value,]
# take the first digit of the species column and put it in a new column
Biom$bloodline = sapply(Biom[,2], function(x) as.numeric(unlist(strsplit(as.character(x), "")))[1])
return(Biom)
}
BiomPhen = NULL
if (phenotype) BiomPhen <- plotB(biomass)
BiomSp <- plotB(biomassTot)
if (phenotype) return(list(BiomSp,BiomPhen)) else return(BiomSp)
}
# function that takes the output of the model and make it usable for plotting and shit
finalTouch <- function(result, dt = 0.1, print_it = T)
{
## processing data
if (print_it) cat("Data handling\n")
# a result will be a list of simulations (number of runs) and the ID card of the ecosystem,
gc()
sim = result[[1]] # get the dat
SummaryParams = result[[2]] # get the params
rm(result) # get space back
sim <- sim[lapply(sim, length) > 0] # if a sim is empty
template = sim[[length(sim)]] # to keep a template of mizer object somewhere
gc()
# stitiching the sims together
Dtime = SummaryParams@species_params$timeMax[1] * dt
Dsp = length(SummaryParams@species_params$ecotype)
Dw = dim(sim[[1]]@n)[3]
# put all the sim at the same dimension
biomList <- list()
for (i in 1:length(sim)) # for each sim
{
biom <- array(data = 0, dim = c(dim(sim[[i]]@n)[1], Dsp, Dw), dimnames = list(dimnames(sim[[i]]@n)$time, SummaryParams@species_params$ecotype, SummaryParams@w)) # create an array of the right dimension
names(dimnames(biom)) = c("time", "species", "size")
for (j in dimnames(sim[[i]]@n)$sp) # fill it when necessary
biom[, which(dimnames(biom)$species == j), ] = sim[[i]]@n[, which(dimnames(sim[[i]]@n)$sp == j), ]
biomList[[i]] <- biom[-1,,] # store it
}
biom <- do.call("abind", list(biomList, along = 1)) # abind the list
names(dimnames(biom)) = list("time", "species", "size")
dimnames(biom)$time = seq(1, SummaryParams@species_params$timeMax[1])*dt
# I have to do the phyto aussi
phyto <- do.call(abind, c(lapply(sim, function(isim)
isim@n_pp), along = 1))
# taking care of the effort
effort <- do.call(rbind, lapply(sim, function(isim)
isim@effort))
names(dimnames(effort)) = list("time", "effort")
dimnames(effort)$time = seq(1, SummaryParams@species_params$timeMax[1])*dt
# reconstruct the mizer object
sim = template
sim@params = SummaryParams
sim@n = biom
sim@effort = effort
sim@n_pp = phyto
rm(list = "biom", "phyto", "effort")
gc()
return(sim)
}
# #function that prep files for download
# DLfig <- function(folder)
# {
# dirContent = dir(folder)
# runFolder = dirContent
# for (i in 1:length(dirContent))
# if (runFolder[i] == "fig" || runFolder[i] == "results")
# runFolder
#
#
#
# dir.create(paste(folder,"/fig",sep=""))
#
#
# }
# function that load runs from a folder and put them in a list for further uses
bunchLoad <- function(folder)
{
dirContent <- dir(folder)
listOfSim = list()
for(i in 1:length(dirContent))
{
if (file.exists(paste(folder,"/",dirContent[i],"/run.Rdata",sep = "")))
{
sim <- get(load(paste(folder,"/",dirContent[i],"/run.Rdata",sep="")))
listOfSim[[i]] = sim
} else if (file.exists(paste(folder,"/",dirContent[i],"/defaultRun.Rdata",sep = "")))
{
sim <- get(load(paste(folder,"/",dirContent[i],"/defaultRun.Rdata",sep="")))
sim = superOpt(sim)
listOfSim[[i]] = sim
}
listOfSim <- listOfSim[lapply(listOfSim,length)>0]
}
return(listOfSim)
}
#function that takes a bunch of run in different folder and plots for each run in their respective folder and also a commom folder of average plots
TotAnalysis <- function(folder)
{
dirContent <- dir(folder)
bigSim = list()
for(i in 1:length(dirContent))
{
if (file.exists(paste(folder,"/",dirContent[i],"/run.Rdata",sep = "")))
{
sim <- get(load(paste(folder,"/",dirContent[i],"/run.Rdata",sep="")))
} else {# (file.exists(paste(folder,"/",dirContent[i],"/defaultRun.Rdata",sep = "")))
sim <- get(load(paste(folder,"/",dirContent[i],"/defaultRun.Rdata",sep="")))
sim = superOpt(sim)
}
bigSim[[i]] = sim
where = paste(folder,"/",dirContent[i],sep = "")
# p = plotDynamics(sim)
# mytitle = paste("BiomassSp",".png", sep = "")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
#
# p = plotSS(sim, time_range = ((sim@params@species_params$timeMax[1]*0.1-500) : (sim@params@species_params$timeMax[1]*0.1-1)) )
# mytitle = paste("SizeSpectrum",".png", sep = "")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
# p = PlotNoSp(sim)
# mytitle = paste("noSp",".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
# p= plotBarSp(sim)
# mytitle = paste("barPhen",".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
#
# p = plotGrowthCurve(sim, generation = 50)
# mytitle = paste("growthCurve",".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
#
# path_to_png = paste(where,"/multi",".png",sep="")
# png(filename=path_to_png,width = 20, height = 20, units = "cm",res = 400)
# plotTraitsMulti(sim)
# dev.off()
# path_to_png = paste(where,"/performance",".png",sep="")
# png(filename=path_to_png,width = 20, height = 20, units = "cm",res = 400)
# plotPerformance(sim)
# dev.off()
# if (rawRun)
# {
# p = plotFood(sim)
# mytitle = paste("food",".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
#
# p = plotUdead(sim)
# mytitle = paste("mortality",".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
# }
rm(sim)
for (i in 1:20) gc()
}
sim = superStich(bigSim)
for (i in 1:20) gc()
where = paste(folder,"/results",sep="")
ifelse(!dir.exists(file.path(where)), dir.create(file.path(where)), FALSE) #create the file if it does not exists
save (bigSim,file = paste(where,"/sim.Rdata",sep=""))
# p = plotDynamics(sim)
# mytitle = paste("BiomassSp",".png", sep = "")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
#
# p = plotSS(sim, time_range = ((sim@params@species_params$timeMax[1]*0.1-500) : (sim@params@species_params$timeMax[1]*0.1-1)) )
# mytitle = paste("SizeSpectrum",".png", sep = "")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
# p = PlotNoSp(bigSim)
# mytitle = paste("noSp",".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
# p= plotBarSp(bigSim)
# mytitle = paste("barPhen",".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
#
# p = plotGrowthCurve(sim, generation = 50)
# mytitle = paste("growthCurve",".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
path_to_png = paste(where,"/multi",".png",sep="")
png(filename=path_to_png,width = 20, height = 20, units = "cm",res = 400)
plotTraitsMulti(sim)
dev.off()
# path_to_png = paste(where,"/performance",".png",sep="")
# png(filename=path_to_png,width = 20, height = 20, units = "cm",res = 400)
# plotPerformance(sim)
# dev.off()
}
# load a big run, optimise it and save it in the same folder
dir.Opt <- function(folder)
{
dirContent <- dir(folder)
for(i in 1:length(dirContent))
{
load(paste(folder,"/",dirContent[i],"/defaultRun.Rdata",sep=""))
sim = superOpt(sim)
save(sim, file = paste(folder,"/",dirContent[i],"/Run.Rdata",sep="") )
rm(sim)
}
}
# take a bunch of sims and get average plots from them
AvgAnalysis <- function(folder,where)
{
dirContent <- dir(folder)
bigSim = list()
for(i in 1:length(dirContent))
{
if (file.exists(paste(folder,"/",dirContent[i],"/run.Rdata",sep = "")))
{
sim <- get(load(paste(folder,"/",dirContent[i],"/run.Rdata",sep="")))
} else {
sim <- get(load(paste(folder,"/",dirContent[i],"/defaultRun.Rdata",sep="")))
sim = superOpt(sim)
}
bigSim[[i]] = sim
rm(sim)
}
sim = superStich(bigSim)
for (i in 1:20) gc()
ifelse(!dir.exists(file.path(paste(getwd(),"/",where,sep=""))), dir.create(file.path(paste(getwd(),"/",where,sep=""))), FALSE) #create the file if it does not exists
save (bigSim,file = paste(where,"/sim.Rdata",sep=""))
p = plotDynamics(sim)
mytitle = paste("BiomassSp",".png", sep = "")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
p = plotSS(sim, time_range = ((sim@params@species_params$timeMax[1]*0.1-500) : (sim@params@species_params$timeMax[1]*0.1-1)) )
mytitle = paste("SizeSpectrum",".png", sep = "")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
p = PlotNoSp(bigSim)
mytitle = paste("noSp",".png",sep="")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
p= plotBarSp(bigSim)
mytitle = paste("barPhen",".png",sep="")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
p = plotGrowthCurve(sim, generation = 50)
mytitle = paste("growthCurve",".png",sep="")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
path_to_png = paste(where,"/multi",".png",sep="")
png(filename=path_to_png,width = 20, height = 20, units = "cm",res = 400)
plotTraitsMulti(sim)
dev.off()
path_to_png = paste(where,"/performance",".png",sep="")
png(filename=path_to_png,width = 20, height = 20, units = "cm",res = 400)
plotPerformance(sim)
dev.off()
}
# to average multiple sims
superStich <- function(listOfSim)
{
# params needed
no_w = 100
min_w = 0.001
max_w = 1e5 * 1.1
min_w_pp = 1e-10
w_pp_cutoff = 1e3
n = 0.75
p = 0.75
q = 0.8
r_pp = 4
kappa = 0.05
lambda = 2+q-n
EcoName = do.call("c",lapply(listOfSim, function(isim) isim@params@species_params$ecotype))
while (sum(duplicated(EcoName))>0) # while there are some duplicated name in the vector
{
for (i in EcoName[duplicated(EcoName)]) # for every duplicated names
{
x <- as.character(EcoName[which(EcoName == i)[2]]) # take the position in the ecotype vect of the duplicated name and extract its name as character
first = as.numeric(unlist(strsplit(x, "")))[1] # extract the first number (meaning species number that we want to keep)
EcoName[which(EcoName == i)[2]] = as.numeric(paste(first,sample(x = seq(1:100000),1),sep="")) #produce a new name but keep the first digit (species identity)
}
}
# Now that I have ecotypes name without duplicate, I can stitch the sims
Sparams = do.call(rbind, lapply(listOfSim, function(isim) isim@params@species_params))
Sparams$ecotype = EcoName
#trait_params <- MizerParams(Sparams, min_w = min_w, max_w=max_w, no_w = no_w, min_w_pp = min_w_pp, w_pp_cutoff = w_pp_cutoff, n = n, p=p, q=q, r_pp=r_pp, kappa=kappa, lambda = lambda)
trait_params <- MizerParams(Sparams,
max_w=1.1*Sparams[length(unique(Sparams$species)),"w_inf"], #w_inf of biggest species at the start
no_w = length(listOfSim[[1]]@params@w),
min_w_pp = listOfSim[[1]]@params@w_full[1],
w_pp_cutoff = round(as.numeric(names(listOfSim[[1]]@params@cc_pp[listOfSim[[1]]@params@cc_pp == 0][1]))),# first size where the background is 0
n = 0.75, p = 0.75, q = 0.8, # I never touch these
r_pp=4, kappa=0.05, #lambda = (2+q-n), # nor these
normalFeeding = F,
tau = 7,
interaction = matrix(data = listOfSim[[1]]@params@interaction[1,1],nrow = dim(Sparams)[1],ncol = dim(Sparams)[1],dimnames = list(Sparams$ecotype,Sparams$ecotype)))
for (i in 1:20) gc()
biom <- do.call(abind, c(lapply(listOfSim, function(isim) isim@n),along = 2))
dimnames(biom)[[2]] = EcoName
names(dimnames(biom)) = list("time","species","w")
# need to balance the abundance of biom -> let's mean everything
biom = biom/length(listOfSim)
sim = listOfSim[[1]]
sim@params = trait_params
sim@n = biom
rm(list = "biom", "listOfSim")
return(sim)
}
# to run in parallel, everything end up in one function for the apply
multiRun <- function(no_sim, no_sp, t_max, mu, no_run,min_w_inf, max_w_inf, effort, print_it = F,
fisheries = F,save_it = F, path_to_save = NULL, normalFeeding = F, hartvig = T, Traits = "eta" )
{
if (fisheries)
{
#asymptotic size
w_inf <- 10^seq(from=log10(min_w_inf), to = log10(max_w_inf), length=no_sp)
#dividing species between the gears (fished and non-fished)
other_gears <- w_inf >= 10000
gear_names <- rep("None", no_sp)
gear_names[other_gears] <- "FishingStuff"
#setting up knife edge
knife_edges <- w_inf * 0.35 # at maturation size
# fisheries are primitve now, so I need to set the knife edge a lot above the size of the species that I do not want to fish
#knife_edges[1:6] <-1e6
output <- myModel(no_sp = no_sp, t_max = t_max, no_run = no_run,
mu = mu, max_w_inf = max_w_inf,
effort = effort, knife_edge_size = knife_edges, gear_names = gear_names,
save_it = save_it, path_to_save = path_to_save,
print_it = print_it, normalFeeding = normalFeeding, hartvig = hartvig, Traits = Traits)
} else {
output <- myModel(no_sp = no_sp, t_max = t_max, no_run = no_run,
mu = mu, max_w_inf = max_w_inf,
effort = 0,
save_it = save_it, path_to_save = path_to_save,
print_it = print_it, normalFeeding = normalFeeding, hartvig = hartvig, Traits = Traits)
}
}
# to lighten the size of the sims
superOpt <- function(object, opt = 10) # optimise the run to make them lighter
{
object@n = object@n[seq(1,dim(object@n)[1],opt),,]
object@n_pp = object@n_pp[seq(1,dim(object@n_pp)[1],opt),]
object@effort = as.matrix(object@effort[seq(1,dim(object@effort)[1],opt),])
return(object)
}
stitch <- function(sim) # this function takes all the sim output from model and paste together the data to make one n file (for plotting and stuff)
{
endList <- length(sim) # shortcut to have ref of the last simulation which has the right dim, names, ...
Dtime = dim(sim[[endList]]@n)[1] # need these dimentions for making new arrays but separatly as I'm going to work on sp dim
Dsp = dim(sim[[endList]]@n)[2]
Dw = dim(sim[[endList]]@n)[3]
# if no mutants
if (endList == 1) return(list(sim[[1]]@n, sim[[1]]@n_pp)) # if there is only one object in the list, it is the mizer object without mutants
# from there I can add empty columns to every other array and add them
for (i in 1:(endList-1))# check every array except last one because no need
{
sim[[i]]@n <-abind(sim[[i]]@n, array(dim = c(Dtime, Dsp - dim(sim[[i]]@n)[2],Dw)), along = 2) # I add to each sim the missing columns pre-mutations
sim[[i]]@n_pp <-abind(sim[[i]]@n_pp, array(dim = c(Dtime, Dsp - dim(sim[[i]]@n)[2])), along = 2)
}
# Now every array in the output has the dimemsions of the final array, but with NAs everywhere
# Now to add them up
# getting rid of the NAs
for (j in 1:endList) sim[[j]]@n[is.na( sim[[j]]@n)] <- 0
for (j in 1:endList) sim[[j]]@n_pp[is.na( sim[[j]]@n_pp)] <- 0
# addition
biomass = 0
for (j in 1:endList) biomass = biomass + sim[[j]]@n
colnames(biomass) <- colnames(sim[[endList]]@n)
names(dimnames(biomass)) <- list("time","sp","w")
biomass_pp = 0
for (j in 1:endList) biomass_pp = biomass_pp + sim[[j]]@n_pp
colnames(biomass_pp) <- colnames(sim[[endList]]@n_pp)
names(dimnames(biomass_pp)) <- list("time","sp")
return(list(biomass,biomass_pp))
}
# to plot only one species family Not sure its working anymore, but function included in plots directly
lineage = function(sim,thread)
{
noSp = length(sim@params@species_params$species) # number of species after the simulation
family = sim@params@species_params[sim@params@species_params$species == thread, ] # create a smaller table with only one lineage
idxFamily = rownames(family)
tree = sim@n[,idxFamily,] # get their biomass from the big table
sim@n = tree
plotDynamics(sim)
}
# to process the mizer output and draw plots, works with the old sim output
processing <- function(result,
plot = FALSE,
where = NULL,
who = "defaultRun",
dt = 0.1, # I need dt at some point and I don't know how to get it there
optimisation = F,
save_it = T
)
{
# setting up workspace things
if (is.null(where)) where = paste(getwd(),"/temporary",sep="")
ifelse(!dir.exists(file.path(where)), dir.create(file.path(where),recursive = T), FALSE) #create the file if it does not exists
## processing data
# a result will be a list of simulations (number of runs) and the ID card of the ecosystem,
# if not it means that I am inputing directly an already processed mizer object, ready for the plots
if (class(result) == "list")
{
if (class(result[[length(result)]]) == "list") #if this is a list it means I got umbrella
{
print("umbrella time")
result[[length(result)]] = NULL # delete last half sim
param = NULL
for (i in 1:length(result)) param = rbind(param,result[[i]]@params@species_params) # create the dataframe for species
param <- param[order(param$ecotype, param$extinct, decreasing=TRUE),]
param <- param[!duplicated(param$ecotype),]
SummaryParams = param[order(param$pop,param$ecotype),]
FinalParam <- MizerParams(SummaryParams, min_w =0.001, max_w=10000 * 1.1, no_w = 100, min_w_pp = 1e-10, w_pp_cutoff = 0.5, n = 0.75, p=0.75, q=0.8, r_pp=4, kappa=1, lambda = 2.05) #create the mizer param from the dataframe
temp=list(result,FinalParam) # put it in the right disposition
result = temp
# something is wrong with it, don't have the time or interest to debug, just return nothing, it's not like it's useful
return()
}
gc()
sim = result[[1]]
SummaryParams = result[[2]]
#endlist = length(sim) # shortcut to have the number of sim
rm(result)
# lol = list() # here I get rid of the empty spots of my list. It will happen if I started a simulation from a previous one as initial conditions
# for (i in 1:endlist) if (!is.null(sim[[i]])) lol = c(lol, sim[[i]])
# sim = lol
# rm(lol)
sim <- sim[lapply(sim,length)>0]
endlist = length(sim) # updating the number
template = sim[[endlist]] # to keep a template of mizer object somewhere
gc()
# if (optimisation)
# {
# keep = seq(1,SummaryParams@species_params$timeMax[1]*0.1) #times to keep (too long but doesnt matter)
# for (i in 1:endlist)
# {
# timeKeep = dimnames(sim[[i]]@n)$time %in% keep
# sim[[i]]@n = sim[[i]]@n[which(timeKeep),,]
# sim[[i]]@n_pp = sim[[i]]@n_pp[which(timeKeep),]
# sim[[i]]@effort = as.matrix(sim[[i]]@effort[which(timeKeep[-1]),]) # effort is being a pain again as it's missing the time 0
# }
# }
if(optimisation)
{
for (i in 1:endlist)
{
sim[[i]]@n = sim[[i]]@n[seq(1,dim(sim[[i]]@n)[1]-1,2),,]
sim[[i]]@n_pp = sim[[i]]@n_pp[seq(1,dim(sim[[i]]@n)[1]-1,2),]
#sim[[i]]@effort = sim[[i]]@effort[seq(1,dim(sim[[i]]@n)[1],2),]
}
}
# stitiching the sims together
Dtime = dim(sim[[1]]@n)[1] # need these dimensions for making new arrays but separatly as I'm going to work on sp dim
Dsp = length(SummaryParams@species_params$ecotype)
Dw = dim(sim[[1]]@n)[3]
# put all the sim at the same dimension
for (i in 1:endlist)
{
# print(i)
for (j in 1:Dsp)
{
gc()
if (is.na(dimnames(sim[[i]]@n)$sp[j])) # if I need to add species at the end of the array
{
sim[[i]]@n <- abind(sim[[i]]@n,array(dim = c(Dtime,Dw)),along = 2)
names(dimnames(sim[[i]]@n)) <- list("time","sp","w") # the abind make me loose the info
}
else if (dimnames(sim[[i]]@n)$sp[j] != SummaryParams@species_params$ecotype[j]) # if I need to add in the middle of the array
{
rmbr = NULL # when I abind only one column of the array (happens when there is an extinction right before the last ecotype), I lose its name, leading to bugs
if (j == dim(sim[[i]]@n)[2]) rmbr = dimnames(sim[[i]]@n)$sp[j] # with this I know when it happens
sim[[i]]@n <- abind(sim[[i]]@n[,1:j-1,],array(dim = c(Dtime,Dw)),sim[[i]]@n[,j:dim(sim[[i]]@n)[2],], along = 2) # if sp 1 goes extinct I will have a bug, or not it seems
names(dimnames(sim[[i]]@n)) <- list("time","sp","w") # the abind makes me loose the info
if (is.null(rmbr)==F) dimnames(sim[[i]]@n)$sp[j+1] = rmbr # and I add back the name
}
}
dimnames(sim[[i]]@n)$sp = SummaryParams@species_params$ecotype # put back the right names
if (optimisation == F) sim[[i]]@n = sim[[i]]@n[-1,,] # in my tentative to include fisheries, I am deleting the first time step (which is 0 now) to have the same length as the effort matrix. I'm running out of time, Ill fix that later I hope
}
# getting rid of the NAs
gc()
for (j in 1:endlist) sim[[j]]@n[is.na( sim[[j]]@n)] <- 0
gc()
biom <- do.call(abind, c(lapply(sim, function(isim) isim@n),along = 1))
gc()
# now add them in a row
# biom = sim[[1]]@n
# for (i in 2:endlist) biom = abind(biom,sim[[i]]@n,along = 1)
names(dimnames(biom)) = list("time","species","size")
if (optimisation) dimnames(biom)$time = seq(0, (SummaryParams@species_params$timeMax[1]-1) *dt,0.2) # skip every other time step and reduce to years (not year/dt)
if (optimisation == F) dimnames(biom)$time = seq(1, SummaryParams@species_params$timeMax[1]) * dt # i don't know whats wrong with the else
gc()
# I have to do the phyto aussi
# phyto = template@n_pp
# phyto = phyto[seq(1,dim(phyto)[1]-1,2),]
# phyta = phyto
# for (i in 2:endlist) phyto = abind(phyto,phyta,along = 1)
if (optimisation)
{
phyto = sim[[1]]@n_pp
for (i in 2:endlist) phyto = abind(phyto,sim[[i]]@n_pp,along = 1)
}
if (optimisation == F)
{
phyto = sim[[1]]@n_pp[-1,] #get rid of time 0
for (i in 2:endlist) phyto = abind(phyto,sim[[i]]@n_pp[-1,],along = 1)
}
cat(sprintf("Biomass handeled successfully, now starting the effort.\n"))
# taking care of the effort
# for now it is constant in time so we just need to multiply effort by no_run
effort = sim[[1]]@effort
for(i in 2:endlist) effort = rbind(effort,sim[[i]]@effort)
names(dimnames(effort)) = list("time","effort")
if (optimisation) dimnames(effort)$time = seq(1, SummaryParams@species_params$timeMax[1] * dt)
if (optimisation == F) dimnames(effort)$time = seq(1, SummaryParams@species_params$timeMax[1]) * dt # i don't know whats wrong with the else
# I need to assemble the mizer object now
sim = template
sim@params=SummaryParams
sim@n = biom
sim@effort = effort
sim@n_pp = phyto
rm(list = "template","biom","phyto","effort")
gc()
#saving
path_to_save <- paste(where,"/",who,".Rdata", sep="")
if (save_it) save(sim,file = path_to_save)
}
else
{
sim = result
rm(result)
#saving
path_to_save <- paste(where,"/",who,".Rdata", sep="")
if (save_it) save(sim,file = path_to_save)
}
if (plot == TRUE)
{
cat(sprintf("Data processed, starting the plots"))
SumPar = sim@params@species_params # little shortcut
# biomass variation
p = plotDynamics(sim, species = T)
mytitle = paste("BiomassSp",".png", sep = "")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
# size spectrum
p = plotSS(sim, time_range = ((sim@params@species_params$timeMax[1]*dt-500) : (sim@params@species_params$timeMax[1]*dt-1)) )
mytitle = paste("SizeSpectrum",".png", sep = "")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
# traits plots
p = plotTraits(sim)
for (i in 1:length(p))
{
for (j in 1:length(p[[1]]))
{
y = as.character(i)
switch(y,
"1" = {mytitle = paste("Maturation size of specie ",j,".png", sep = "")},
"2" = {mytitle = paste("PPMR of specie ",j,".png", sep = "")},
"3" = {mytitle = paste("Diet breadth of specie ",j,".png", sep = "")},
{})
ggsave(mytitle, plot = p[[i]][[j]], path = where, width = 20, height = 20, units = "cm")
}
}
# # beta/sigma ratio---------------
# for (i in SpIdx)
# {
# # empty matrix of ecotype of species i by time
# A = matrix(0, ncol = SumPar$timeMax[1], nrow = dim(TT[TT$Lineage == i,])[1], dimnames = list(as.numeric(TT$Ecotype[TT$Lineage == i]), c(1:(SumPar$timeMax[1]))))
# # fill the matrix with ones when the ecotype exists
# for (x in 1:nrow(A)) # I'm sure I can do an apply but don't know how
# {
# for (j in 1:ncol(A))
# {
# if (TT$Apparition[x] <= j & TT$Extinction[x] >= j) A[x,j] = 1
# }}
# # a is a matrix of 0 and 1 showing if the ecotype is present or not at time t
# # change the ones by the trait value of the ecotype
# BetaA = A * TT[TT$Lineage == i,]$PPMR
# SigmaA = A * TT[TT$Lineage == i,]$Diet_breadth
# # calculate mean trait value at each time step
# no_trait = apply(A,2,sum) # this vector is the number of traits present at each time step
# BetaSum = apply(BetaA,2,sum) # this vector is the sum of the traits value at each time step
# SigmaSum = apply(SigmaA,2,sum) # this vector is the sum of the traits value at each time step
# BetaMean = BetaSum/no_trait # this is the mean trait value at each time step
# SigmaMean = SigmaSum/no_trait # this is the mean trait value at each time step
#
# # Matrix with all traits combination and at what time they go extinct
# TTi = TT[TT$Lineage == i,]
# comb=data.frame(TTi$Ecotype,TTi$Apparition,TTi$Extinction,TTi$PPMR,TTi$Diet_breadth)
#
# # plot of extinction of combinations
# title = paste("Combination of PPMR and diet breath value of species ",i, sep = "")
# print(
# ggplot(comb) +
# geom_point(aes(x=TTi.PPMR,y=TTi.Diet_breadth, color = TTi.Extinction)) +
# scale_x_continuous(name = "PPMR") +
# scale_y_continuous(name = "Diet breath") +
# scale_color_continuous(name = "Extinction time in year / dt") +
# ggtitle(title)
# )
# name = paste("Extinction BetaSigma of species",i, sep = "")
# setwd(where)
# mytitle = paste(name,".png", sep = "")
# dev.print(png, mytitle, width = res, height = 2/3* res)
#
# #plot of apparition of combinations
# print(
# ggplot(comb) +
# geom_point(aes(x=TTi.PPMR,y=TTi.Diet_breadth, color = TTi.Apparition)) +
# scale_x_continuous(name = "PPMR") +
# scale_y_continuous(name = "Diet breath") +
# scale_color_continuous(name = "Apparition time in year / dt") +
# ggtitle(title)
# )
# name = paste("Apparition BetaSigma of species",i, sep = "")
# setwd(where)
# mytitle = paste(name,".png", sep = "")
# dev.print(png, mytitle, width = res, height = 2/3* res)
#
#
# # Mean combination values throughout sim
# stat = data.frame(BetaMean,SigmaMean)
# #get rid of duplicates
# stat = stat[!duplicated(stat),]
# # need to work on the time because of fucked up legend
# stat = cbind(stat,rownames(stat))
# dimnames(stat)[[2]] = list("BetaMean", "SigmaMean", "Time")
# stat$Time <- as.numeric(substr(stat$Time,1,5)) # read the time and delete all conditions on it (like factor) # the 5 means handling number up to 10^5
#
# title = paste("Mean trait's combination of PPMR and diet breath value of species ",i, sep = "")
# print(
# ggplot(stat) +
# geom_point(data = stat, aes(x=BetaMean,y=SigmaMean, color = Time)) +
# scale_x_continuous(name = "PPMR") +
# scale_y_continuous(name = "Diet breath") +
# scale_color_continuous(name = "Time in year / dt") +
# ggtitle(title)
# )
# name = paste("Mean BetaSigma of species",i, sep = "")
# setwd(where)
# mytitle = paste(name,".png", sep = "")
# dev.print(png, mytitle, width = res, height = 2/3* res)
# }
#
# biology plots -------------
# when = seq(500,sim@params@species_params$timeMax[1]*0.1,1000)
# for(i in when)
# {
# p = plotFood(sim, time_range = i)
# mytitle = paste("feeding t= ",i,".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
#
# p = plotUdead(sim, time_range = i)
# mytitle = paste("mortality t= ",i,".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
#
# p = plotGrowth(sim, time_range = i)
# mytitle = paste("growth t= ",i,".png",sep="")
# ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
# }
p = plotFood(sim)
mytitle = paste("food",".png",sep="")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
p = plotUdead(sim)
mytitle = paste("mortality",".png",sep="")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
p = PlotNoSp(sim)
mytitle = paste("noSp",".png",sep="")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
p = plotGrowthSpeed(sim)
mytitle = paste("growthSpeed",".png",sep="")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
p = plotGrowthCurve(sim, generation = 50)
mytitle = paste("growthCurve",".png",sep="")
ggsave(mytitle, plot = p, path = where, width = 20, height = 20, units = "cm")
path_to_png = paste(where,"/multi",".png",sep="")
png(filename=path_to_png,width = 20, height = 20, units = "cm",res = 400)
plotTraitsMulti(sim)
dev.off()
}
return(sim)
}
# function that draw the bloodline of an ecotype by plotting its trait combination (could do 3D) Need work
ancestor <- function ()
{
SumPar = sim@params@species_params
TT = cbind(SumPar$species,as.numeric(SumPar$ecotype),SumPar$pop,SumPar$extinct,SumPar$w_mat,SumPar$beta,SumPar$sigma) # weird things happen without the as.numeric
colnames(TT) = c("Lineage","Ecotype","Apparition","Extinction","Maturation_size","PPMR","Diet_breadth")
rownames(TT) = rownames(SumPar)
TT = TT[order(TT[,1],decreasing=FALSE),]
TT = as.data.frame(TT)
for (i in 1:dim(TT)[1]) if (TT$Extinction[i] == 0) TT$Extinction[i] = SumPar$timeMax[1]
res = 1000
i = 1 # sp
# empty matrix of ecotype of species i by time
A = matrix(0, ncol = SumPar$timeMax[1], nrow = dim(TT[TT$Lineage == i,])[1], dimnames = list(as.numeric(TT$Ecotype[TT$Lineage == i]), c(1:(SumPar$timeMax[1]))))
# fill the matrix with ones when the ecotype exists
for (x in 1:nrow(A)) # I'm sure I can do an apply but don't know how
{
for (j in 1:ncol(A))
{
if (TT$Apparition[x] <= j & TT$Extinction[x] >= j) A[x,j] = 1
}}
# a is a matrix of 0 and 1 showing if the ecotype is present or not at time t
# change the ones by the trait value of the ecotype
BetaA = A * TT[TT$Lineage == i,]$PPMR
SigmaA = A * TT[TT$Lineage == i,]$Diet_breadth
# calculate mean trait value at each time step
no_trait = apply(A,2,sum) # this vector is the number of traits present at each time step
BetaSum = apply(BetaA,2,sum) # this vector is the sum of the traits value at each time step
SigmaSum = apply(SigmaA,2,sum) # this vector is the sum of the traits value at each time step
BetaMean = BetaSum/no_trait # this is the mean trait value at each time step
SigmaMean = SigmaSum/no_trait # this is the mean trait value at each time step
# Matrix with all traits combination and at what time they go extinct
TTi = TT[TT$Lineage == i,]
comb=data.frame(TTi$Ecotype,TTi$Apparition,TTi$Extinction,TTi$PPMR,TTi$Diet_breadth)
tree = NULL
for (j in 2:dim(comb)[1]) # for each fucking ecotype (except first)
{
x = comb[j, "TTi.Ecotype"]
bloodLine = x
while (x != i) # I determine from whom they descend
{
x = x %/% 10
bloodLine = c(bloodLine, x)
}
ancestor = NULL
for (l in 1:dim(comb)[1]) # and I put all these people in one matrix
{
for (k in 1:length(bloodLine))
{
if (comb$TTi.Ecotype[l] == bloodLine[k])
ancestor = rbind(ancestor, comb[l, ])
}
}
ancestor = cbind(ancestor, j)
tree = rbind(tree,ancestor)
}
print(
ggplot(comb) +
geom_point(aes(x=TTi.PPMR,y=TTi.Diet_breadth, color = TTi.Apparition, size = 1) ) +
geom_path(data = tree, aes(x = TTi.PPMR, y = TTi.Diet_breadth, group = j), arrow = arrow(angle = 15, type = "closed", length = unit(0.1,"inches"))) +
scale_x_continuous(name = "PPMR") +
scale_y_continuous(name = "Diet breath") +
scale_color_continuous(name = "Apparition time in year / dt") +
ggtitle(title)
)
# plot of extinction of combinations
title = paste("Combination of PPMR and diet breath value of species ",i, sep = "")
print(
ggplot(comb) +
geom_point(aes(x=TTi.PPMR,y=TTi.Diet_breadth, color = TTi.Extinction)) +
scale_x_continuous(name = "PPMR") +
scale_y_continuous(name = "Diet breath") +
scale_color_continuous(name = "Extinction time in year / dt") +
ggtitle(title)
)
name = paste("Extinction BetaSigma of species",i, sep = "")
setwd(paste(dir,subdir, sep = ""))
mytitle = paste(name,".png", sep = "")
dev.print(png, mytitle, width = res, height = 2/3* res)
#plot of apparition of combinations
print(
ggplot(comb) +
geom_point(aes(x=TTi.PPMR,y=TTi.Diet_breadth, color = TTi.Apparition)) +
geom_path(data = tree, aes(x = TTi.PPMR, y = TTi.Diet_breadth, group = j)) +
scale_x_continuous(name = "PPMR") +
scale_y_continuous(name = "Diet breath") +
scale_color_continuous(name = "Apparition time in year / dt") +
ggtitle(title)
)
name = paste("Apparition BetaSigma of species",i, sep = "")
setwd(paste(dir,subdir, sep = ""))
mytitle = paste(name,".png", sep = "")
dev.print(png, mytitle, width = res, height = 2/3* res)
# Mean combination values throughout sim
stat = data.frame(BetaMean,SigmaMean)
#get rid of duplicates
stat = stat[!duplicated(stat),]
# need to work on the time because of fucked up legend
stat = cbind(stat,rownames(stat))
dimnames(stat)[[2]] = list("BetaMean", "SigmaMean", "Time")
stat$Time <- as.numeric(substr(stat$Time,1,5)) # read the time and delete all conditions on it (like factor) # the 5 means handling number up to 10^5
title = paste("Mean trait's combination of PPMR and diet breath value of species ",i, sep = "")
print(
ggplot(stat) +
geom_point(data = stat, aes(x=BetaMean,y=SigmaMean, color = Time)) +
scale_x_continuous(name = "PPMR") +
scale_y_continuous(name = "Diet breath") +
scale_color_continuous(name = "Time in year / dt") +
ggtitle(title)
)
# all present traits at last time step
# last = data.frame(BetaA[,t_max*no_run],SigmaA[,t_max*no_run])
# colnames(last) = c("beta","sigma")
# row_sub = apply(last, 1, function(row) all(row !=0 ))
# last = last[row_sub,]
#
#
# print(
# ggplot(last) +
# geom_point(aes(x=beta,y=sigma)) +
# ggtitle("Last time step")
# )
}
#function that reduce the number of data if too heavy for plotting
optimisation <- function(sim, keep = 2)
{
sim@n = sim@n[seq(1,dim(sim@n)[1],keep),,]
sim@n_pp = sim@n_pp[seq(1,dim(sim@n_pp)[1],keep),]
sim@effort = sim@effort[seq(1,dim(sim@effort)[1],keep),]
return(sim)
}
# # old trait tree function included in processing but keeping it just in case (has not weighted plots) -----------------
# #TT = cbind(sim@params@species_params$species,sim@params@species_params$pop,sim@params@species_params$extinct,sim@params@species_params$w_mat,sim@params@species_params$beta,sim@params@species_params$sigma)
# TT = cbind(SumPar$species,as.numeric(SumPar$ecotype),SumPar$pop,SumPar$extinct,SumPar$w_mat,SumPar$beta,SumPar$sigma) # weird things happen without the as.numeric
# colnames(TT) = c("Lineage","Ecotype","Apparition","Extinction","Maturation_size","PPMR","Diet_breadth")
# rownames(TT) = rownames(SumPar)
# TT = TT[order(TT[,1],decreasing=FALSE),]
# TT = as.data.frame(TT)
# no_run = 25 # need to specify that if I did more than one succession
# for (i in 1:dim(TT)[1]) if (TT$Extinction[i] == 0) TT$Extinction[i] = SumPar$timeMax[1]
# res = 1000
# #plot of traits maturation size