Deliver geospatial models direct to decision makers with SyncroSim › Forums › ST-Sim & State-and-Transition Simulation Models › ST-Sim Library with an external program example
- This topic has 4 replies, 4 voices, and was last updated 2 years, 1 month ago by leonardo-frid.
-
AuthorPosts
-
September 16, 2022 at 8:34 pm #21203katie-birchardKeymaster
A user recently requested an example of a simple ST-Sim library that runs an R script interactively.
We created a very simple worked example based on the ST-Sim spatial template library that uses the External Program feature. During the simulation, at the beginning of each timestep, ST-Sim will pause and pass an output raster to an R script. The R script will then generate a spatial transition multiplier raster based on the output raster and pass it back to ST-Sim. ST-Sim will then simulate the next timestep and so-on.
The example ST-Sim Library is set up to pass the output age raster to an R script at each timestep which generates a spatial transition multiplier for fire transitions. The oldest 20 cells in the age raster are assigned to 1 in the spatial transition multiplier, while all other cells are assigned to 0. This means that the oldest 20 cells at each timestep are forced to undergo a fire transition. Below are instructions on how to run this example.
We created this example using the following software and packages:- SyncroSim version 2.3.17
- ST-Sim version 3.3.9
- R version 4.2.1
- R packages (Note these need to be pre-installed through R):
- rsyncrosim version 1.3.2
- raster version 3.5.29
- dplyr version 1.0.10
- stringr version 1.4.1
- rgdal version 1.5.32
First, download the ST-Sim Library and external program R script here. Open the ST-Sim Library in a new SyncroSim session and make sure to store the external program R script in an accessible location on your computer.
To run an R script interactively in ST-Sim, you will need to use the External Program feature. This feature can be accessed from the SyncroSim User Interface using the following steps:
- Right-click on the Scenario called “Burn 20 oldest cells” and select Properties from the dropdown menu.
- Navigate to the Advanced tab.
- Select External Program from the menu on the left-hand side.
The External Program Datasheet contains the following inputs:
- External program: the file path to your Rscript executable (e.g., “C:\Program Files\R\R-4.2.1\bin\Rscript.exe”). Note that you will need to change the file path to your own Rscript executable here.
- External script: the file path to your external program script. Note that you will need to change the file path to wherever you stored the downloaded external program R script here.
- Call before timesteps: the timesteps where the simulation is paused to run the external program R script. Because we have values 1-3 selected, this means that the simulation will pause to run the R script before timesteps 1, 2, and 3.
After adding the external program and external script file paths, save the Library. Now, if you right-click on the Scenario and select Run, the external program will be invoked before each timestep. Once the run has completed, you can select the Fire Transitions map from the Results Viewer.
Each of these raster maps contains the 20 oldest cells that were forced to undergo a fire transition, as carried out by our external program R script.
- This topic was modified 2 years, 2 months ago by katie-birchard.
- This topic was modified 2 years, 2 months ago by katie-birchard.
September 18, 2022 at 12:51 am #21208colin-danielKeymasterAs with everything in SyncroSim, you can also do all this using an R or Python script (instead of through the UI) using the corresponding rsyncrosim or pysyncrosim packages.
October 21, 2022 at 9:05 pm #21295davidandersonParticipantAre there any examples of how this might work with pysyncrosim? Without having done it I have good idea how it should work but without trying it I’m a bit fuzzy on the details.
October 25, 2022 at 6:31 pm #21302katie-birchardKeymasterThere will be an example of an external program script using pysyncrosim forthcoming.
November 1, 2022 at 6:47 pm #21384leonardo-fridKeymasterThe steps to run an External Program using a Python script are similar to the above example, with a few minor changes outlined below.
The example was created using the following software and packages:
- SyncroSim version 2.4.10
- ST-Sim version 3.3.9
- Python version 3.10.6
- Python packages (Note these need to be pre-installed using conda or pip):
- pysyncrosim version 1.0.19
- pandas version 1.5.1
- numpy version 1.23.4
- rasterio version 1.3.3
You can use the same ST-Sim Library as the above example. However, you will also need to download the external program Python script example here.
Update the External Program Datasheet within the Scenario Properties to include your Python executable file and Python script.
Follow the remaining instructions above to retrieve the desired outputs.
- This reply was modified 2 years, 1 month ago by katie-birchard.
- This reply was modified 2 years, 1 month ago by leonardo-frid.
- This reply was modified 2 years, 1 month ago by katie-birchard.
-
AuthorPosts
- You must be logged in to reply to this topic.