Niche Vlaanderen can be used interactively in Python. For interactive use, we recommend using the Jupyter notebook. This allows one to save documentation, code and results in the same file.
This file itself is a notebook, and if you want to reproduce our result this is possible:
docs
folder from the anaconda prompt. Make sure you also extract the testcases directory, as this contains the data we will be using.activate niche
(not necessary if you used the alternative install).jupyter notebook
. This should open a web page (similar or equal to http://localhost:8888 ) - check your anaconda prompt for a link if this is not the case.getting_started.ipynb
file (in the Files tab, which should be opened by default).Help
menu.To calculate a Niche model one has to take the following steps:
Niche
object)set_input
method)run
method)plot()
and table
.write
method).These steps are mirrored in the design of the Niche
class which is given below.
Optionally the user can also create difference maps showing how much MHW/MLW has
to change to allow a certain vegetation type. This is done using the deviation
parameter of the run
method.
For our first example, we will be creating a simple model, using only MHW, MLW and soil for the predictions.
The first step is importing the niche_vlaanderen
module. For convenience, we will be importing as nv
.
In [ ]:
import niche_vlaanderen as nv
In [ ]:
simple = nv.Niche()
In [ ]:
simple.set_input("mhw","../testcase/zwarte_beek/input/mhw.asc")
simple.set_input("mlw","../testcase/zwarte_beek/input/mlw.asc")
simple.set_input("soil_code","../testcase/zwarte_beek/input/soil_code.asc")
In [ ]:
simple.run(full_model=False)
After a model is run, we can inspect the results using the table
method. Note that the values are given in ha. In the example below we also use the head function to show only the first five rows.
In [ ]:
simple.table.head()
The returned object is a Pandas dataframe which makes it easy to manipulate (for example calculating a crosstabulation, filtering, ...) or save it to a file. Below we present two examples which can be useful when working with these data. The first is saving the data to a csv file.
In [ ]:
simple.table.to_csv("demo.csv")
By using the pandas pivot_table method, we can create a summarized table. Note that only the first 5 rows are shown because we use the head function
In [ ]:
result = simple.table.pivot_table(index="vegetation",
values="area_ha",
columns="presence",
fill_value=0).head()
result
It is also possible to show actual grids using the plot
method.
In [ ]:
simple.plot(11)
import matplotlib.pyplot as plt
%matplotlib inline
plt.show()
It is possible to give your model a name
- this will be shown when plotting and will be used when writing the files.
In [ ]:
simple.name="scen_1"
simple.plot(11)
plt.show()
The model results can be saved to disk using the write
function. As an argument, this takes the directory to which you want to save, _output_scen1 in our example. When saving a model, the log file, containing the model configuration, a summary table and all 28 vegetation grids will be saved.
Note we specify the overwrite_files
option so subsequent calls of the example will not raise an error
In [ ]:
simple.write("_output_scen1", overwrite_files=True)
Below you can see the list of files that is generated by this operation: There are the 28 vegetation grids (*.tif
files), the summary table (summary.csv
) and a logfile (log.txt
). All files are prepended with the model name
we set earlier.
In [ ]:
import os
os.listdir("_output_scen1")
In [ ]:
simple
A full Niche model requires more inputs that only mhw, mlw and soil_code. The full list can be found in the documentation. It is also possible to look at the minimal_input
set. When trying to run a model without sufficient inputs, a warning will be generated.
In [ ]:
nv.niche.minimal_input()
If we add all the required values, we can run the full model. Note that it is possible to set a constant value instead of a complete grid
In [ ]:
full = nv.Niche()
path = "../testcase/zwarte_beek/input/"
full.set_input("mhw", path + "mhw.asc")
full.set_input("mlw", path + "mlw.asc")
full.set_input("soil_code", path + "soil_code.asc")
full.set_input("nitrogen_animal", 0)
full.set_input("inundation_acidity", path + "inundation.asc")
full.set_input("inundation_nutrient", path + "inundation.asc")
full.set_input("nitrogen_fertilizer",0)
full.set_input("minerality", path + "minerality.asc")
full.set_input("management", path + "management.asc")
full.set_input("nitrogen_atmospheric", 0)
full.set_input("msw", path + "msw.asc")
full.set_input("rainwater", 0)
full.set_input("seepage", path + "seepage.asc")
full.run()
We can look at the full model using the same table
and plot
functions as we used for the simple model.
In [ ]:
full.table.head()
Comparing to the simple model, one can observe that the area where a vegetation type can be present is always smaller than in the simple model.
In [ ]:
simple.table.head()
In the next tutorial, we will focus on more advanced methods for using the package, starting with the comparison of these two models.