Skip to content

Commit

Permalink
Merge pull request #41 from Quetzal-framework/develop
Browse files Browse the repository at this point in the history
doc: adding example sources for deterministic and stochastic local process
  • Loading branch information
Becheler authored Jul 2, 2024
2 parents 6bb5f9d + 224b641 commit 3b9234d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/4-tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,8 @@ We build up on the previous example by adding some stochasticity in the process.
Environmental niche models focus on how environmental factors influence the distribution and abundance of species within specific habitats. These models integrate ecological niche theory and environmental data to predict species' occurrence probabilities and population densities across spatial gradients. By quantifying the relationships between environmental variables (e.g., temperature, precipitation, habitat quality) and species' ecological requirements, environmental niche models help elucidate how environmental factors shape local population growth and species distributions.
In this example, we store a time-series vector at each vertex of the graph to monitor the population dynamics. After initializing one vertex with a non-zero value, we iterate through time to update the series conditionally to the local contemporary environmental data. It's important to note that, since we defined no migration along the graph's edges, all time-series except one remain at zero.
**Input**
@include{lineno} geography_graph_local_process_3.cpp
Expand Down
40 changes: 40 additions & 0 deletions example/geography_graph_local_process_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "quetzal/geography.hpp"

namespace geo = quetzal::geography;

int main()
{
auto file = std::filesystem::current_path() / "data/bio1.tif";

// The raster have 10 bands that we will assign to 2001 ... 2010.
std::vector<int> times(10);
std::iota(times.begin(), times.end(), 2001);

// Landscape construction injecting times information
auto land = geo::raster<>::from_file(file, times);

// Graph construction with vertex and edge info
using vertex_info = std::vector<double>;
vertex_info N(times.size(), 0);

using edge_info = geo::no_property;
auto graph = geo::from_grid(land, N, edge_info(), geo::connect_fully(), geo::isotropy(), geo::mirror());
graph[0][0] = 1;

// Part of what happens in a demographic simulator:
for( size_t t = 1; t < times.size(); ++t){
for( auto x : graph.vertices() ){
graph[x][t] = 2 * graph[x][t-1];
}
}

// Post treatment
std::cout << "Time series:\n";
for(auto x : graph.vertices()){
std::cout << "at vertex " << x << " : ";
for(auto value : graph[x] ){
std::cout << value << " ";
}
std::cout << "\n";
}
}
50 changes: 50 additions & 0 deletions example/geography_graph_local_process_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "quetzal/geography.hpp"
#include <random>
#include <cmath>

namespace geo = quetzal::geography;

int main()
{
auto file = std::filesystem::current_path() / "data/bio1.tif";

// The raster have 10 bands that we will assign to 2001 ... 2010.
std::vector<int> times(10);
std::iota(times.begin(), times.end(), 2001);

// Landscape construction injecting times information
auto land = geo::raster<>::from_file(file, times);

// Graph construction with vertex and edge info
using vertex_info = std::vector<double>;
vertex_info N(times.size(), 0);

using edge_info = geo::no_property;
auto graph = geo::from_grid(land, N, edge_info(), geo::connect_fully(), geo::isotropy(), geo::mirror());

// Let's compare some time series
graph[0][0] = 1;
graph[1][0] = 1;
graph[2][0] = 1;

// Declare a random number generator
std::mt19937 rng{std::random_device{}()};

// Part of what happens in a demographic simulator:
for( size_t t = 1; t < times.size(); ++t){
for( auto x : graph.vertices() ){
auto alpha = std::uniform_real_distribution<double>(1, 2)(rng);
graph[x][t] = alpha * graph[x][t-1];
}
}

// Post treatment
std::cout << "Time series:\n";
for(auto x : graph.vertices()){
std::cout << "at vertex " << x << " : ";
for(auto value : graph[x] ){
std::cout << static_cast<int>(value) << " ";
}
std::cout << "\n";
}
}

0 comments on commit 3b9234d

Please sign in to comment.