Skip to content
kalden edited this page Mar 20, 2013 · 4 revisions

7. Suite of R Routines to Analyse 2D Simulations

A number of general routines for visualisation and post-processing have been written for R – thanks to Susanne Schmidt and Edd Miles. These scripts make use of several packages that you may need to install first, using the install.packages('packagename') syntax. The packages required are: vegan, ReadImages and stringr. The R routines are stored in the 'results_analysis_R' folder.

The R scripts read in the data from the csv files rather than XML files as the Matlab scripts do, therefore some of the information is harder to obtain and at the moment has to be supplied by hand. First, the simulation time information is lost and will have to be calculated manually. Second, many of the plots use the number of the output file in a sorted list of output files rather than the iteration number coded in the file name. This means manual transformation is needed (e.g. if there were three output files, agent_State(0).csv, agent_State(20).csv and agent_State(40).csv, the data in these for species abundance would be plotted as 1,2,3). Third, the names of the species and bulks are not read in from the CSV files and must be manually converted to and from the identification number.

Reading in Simulation Data

The first functions read in the data. These are readStates('path_to_csv_files') and readEnv('path_to_env_csv_files'), which will read in all the agent_State and env_State files in the folder given by path (by default, this is “.”, i.e. the current working directory), and output them as a matrix of lists.

Examples:

aStates<- readStates()

eStates<- readEnv('~/Results/2010')

Graphing and Analysis Using R

The following functions can then utilise this data:

plotAgents(agent_state_data): This routine will plot all the agents for a particular iterate using the ‘agent_State’ file, useful for examining the location of each species within the biofilm. Each agent is represented by a circle coloured for each species, but note that the sizes of the circles DO NOT correspond to the actual sizes of the agents. If you want a plot of the agents with the correct agent sizes, use POV-Ray to render the pov file outputted by iDynoMiCS. You can pass this function either the number of the iteration file you want read in (in the current folder), or you can pass it the variable (a list) containing the data you have already read (If you read all the files in using the readState or readEnv functions, this can be achieved by plotAgents(Name_of_agent_variable[iteration_no,]).

Examples:

plotAgents(aStates[1,])

plotAgents(0)

plotTimeCourseAgents(agent_state_data): This will plot the abundance of each species at each iteration. If you pass this no arguments, it will read in all the agent state CSV files in the current folder. Otherwise, it will work with the list of states you pass to it. It will output not only the graph, but also the species abundances, that can be fed into simpsonIndex() as described below.

Examples:

abundances <- plotTimeCourseAgents(aStates)

abundances <- plotTimeCourseAgents()

simpsonIndex(abundance). This will plot the diversity of the community at each iteration and return the numbers as well.

Examples:

diversity <- simpsonIndex(abundances)

plotTimeCourseAbund(AgentStates): This routine plots the sum of all the abundances of all species at each iteration. If you pass this no arguments, it will read in all the agent state CSV files in the current folder. Otherwise, it will work with the list of states you pass to it.

Examples:

plotTimeCourseAbund(aStates)

plotTimeCourseAbund()

plotContour(iterate, solute): This routine draws a contour graph of the solute concentration field at iteration iterate. Iterate can either be the number of the file you want to read in, or a list object already present, solute should be the numeric ID of the solute you want the contours for.

Examples:

ctours <- plotContour(aStates[1,], 1)

ctours <- plotContour(0,1)

overlay(picture, contours): This routine takes an image rendered in POV-Ray and plots the contour data on top. Picture is the path to the image (image must be in jpg format), and contours is the data returned by plotContour().

Example:

Overlay('~/it(0).jpg', ctours)