Extracting Spatital Dynamics of the simulation as a video file

46 views
Skip to first unread message

Soumitra Bhide

unread,
Aug 19, 2021, 2:34:28 AM8/19/21
to slim-discuss

Hello,

I was wondering if there's any direct way of exporting spatial dynamics of a run from SLiMgui in a video format, other than screen recording

Any help would be appreciated :)

Miguel de Navascués

unread,
Aug 19, 2021, 4:26:20 AM8/19/21
to slim-d...@googlegroups.com
Maybe this can be useful (I haven't tried it myself) :

https://twitter.com/fleventy5/status/1411759179409338375

https://bodkan.net/slendr

https://github.com/bodkan/smbe2021
> --
> SLiM forward genetic simulation: http://messerlab.org/slim/
> <http://messerlab.org/slim/>
> ---
> You received this message because you are subscribed to the Google
> Groups "slim-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to slim-discuss...@googlegroups.com
> <mailto:slim-discuss...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/slim-discuss/e46f78b7-bec6-40d6-a7fa-7fbb05251b19n%40googlegroups.com
> <https://groups.google.com/d/msgid/slim-discuss/e46f78b7-bec6-40d6-a7fa-7fbb05251b19n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Miguel de Navascués

UMR CBGP, INRAE
Centre de Biologie pour la Gestion des Populations
755 avenue du campus Agropolis
CS30016
34988 Montferrier-sur-Lez cedex (France)

phone: +33499623370
fax: +33499623345
e-mail: miguel.navascues AT inrae.fr

Martin Petr

unread,
Aug 20, 2021, 1:27:36 PM8/20/21
to slim-discuss
Hi all,

First of all, thanks Ben for bringing this thread to my attention and thanks Miguel for advertising my work :)

I am indeed the author of the slendr R package for programming / visualising / analysing explicitly spatial SLiM models that Miguel linked to.

Soumitra Bhide wrote:
I was wondering if there's any direct way of exporting spatial dynamics
of a run from SLiMgui in a video format, other than screen recording

The slendr package does indeed have some rudimentary functionality for recording the position of every individual who ever lived in the simulation, and recapitulating the simulation run as an animated GIF (an example of which can be seen here: https://bodkan.net/slendr/#8-re-capitulate-the-slim-run-as-an-individual-based-animation). I say "rudimentary" because I initially developed it for debugging purposes and the functionality is rather barebones.

The slendr R package supports (and can load the results of) only SLiM models programmed using slendr itself and it's likely to stay that way. That said, you could easily hack together the same thing I did in the animation from the link above by extracting a) the parts of the SLiM script that's built into the package which saves locations of individuals over time, and b) visualising the outputs of a) using an R function that's part of the package. The whole slendr codebase is MIT licensed, so nothing prevents you from using this :)

The relevant bits of code are quite simple:


b) The GIF animation is created using a custom function `animate()` in my R package. This simply reads the table generated by a) and uses ggplot2 & gganimate to create the GIF animation: https://github.com/bodkan/slendr/blob/63ac64ad8caf8207f67fa754ef497a69e2330b58/R/visualization.R#L305 You could safely ignore the reprojection of coordinates and filtering and editing of the locations table because those are needed only for slendr. The only important parts are loading the table, creating the plot with something like ggplot() + geom_point(data = locs, aes(x, y))` (I forget the exact format of the table, but I think the location columns are named x and y), and then animating the ggplot with something similar to what's on the lines 366-379.

Actually, now that I'm thinking about this, perhaps it's better to simply start from the documentation to gganimate at https://gganimate.com because 80% of what the function does is specific to slendr internals and must be confusing to anyone who's not familiar with the slendr codebase.

Hope this helps!

--Martin


Soumitra Bhide

unread,
Aug 20, 2021, 8:49:57 PM8/20/21
to slim-discuss
Thank you for your quick responses !

I would definitely look into this !

Cheers
Soumitra

Martin Petr

unread,
Aug 23, 2021, 5:21:18 AM8/23/21
to slim-discuss

Good luck!

One thing I will mention which confused me quite a bit is that the selection of an "engine" for the GIF generation in gganimate is a little opaque. The documentation mentions some gifski Rust program and I assumed that's a requirement for generating the GIF. It is not. Installing the magick R package (https://cran.r-project.org/web/packages/magick/index.html) is enough to generate GIFs by gganimate, but AFAIK gganimate does not install it as its dependency. Without it, it only generates a series of PNG images, but doesn't compile them to a GIF file. I guess this makes sense because there are a number of tools to generated animated images and it doesn't want to enforce one particular program. It surprised me when my package generated animated GIFs on my machine but it didn't for other people -- missing magick package turned out to be the problem.

Just in case the same happens to you...

r.di...@gmail.com

unread,
Aug 24, 2021, 11:19:26 PM8/24/21
to slim-discuss
Hi Soumitra,

You can also accomplish this using my new R package {slimr}. I just whipped up a minimal spatial simulation example and posted it here to give you an example: https://rdinnager.github.io/slimr_resources/slimr_animating_a_simple_spatial_simulation.html. {slimr} makes it easy to write a SLiM script in R, run them, and get data in and out of them (see this preprint for more details on the motivation; and here is the package website). You would have to translate your simulation into slimr code though, but it is pretty straightforward: Just wrap your SLiM blocks in calls to `slim_block`, and then wrap up all you slim_blocks into a `slim_script` call. Here is a minimal example from the above linked vignette:

N <- 100

## minimal spatial sim
spat_sim <- slim_script(
 
  slim_block(initialize(), {
    initializeSLiMOptions(dimensionality = "xy")
    initializeMutationRate(1e-7)
    initializeMutationType("m1", 0.5, "f", 0.0)
    initializeGenomicElementType("g1", m1, 1.0)
    initializeGenomicElement(g1, 0, 100000 - 1)
    initializeRecombinationRate(1e-8)
  }),
 
  slim_block(1, early(), {
    sim.addSubpop("p1", !!N)
    sim.addSubpop("p2", !!N)
    p1.setSpatialBounds(c(0, 0, 1, 1))
    p2.setSpatialBounds(c(0, 0, 1, 1))
    p1.individuals.setSpatialPosition(p1.pointUniform(!!N))
    p2.individuals.setSpatialPosition(p2.pointUniform(!!N))
  }),
 
  slim_block(1, 1000, late(), {
    inds = sim.subpopulations.individuals
    location = inds.spatialPosition
    ## move around a bit
    inds.setSpatialPosition(p1.pointReflected(sim.subpopulations.individuals.spatialPosition +
                                               rnorm(inds.size() * 2, 0, 0.01)))
    slimr_output_full() ## <- this bit is unique to slimr, it is how you get data out of SLiM into R
  }),
 
  slim_block(modifyChild(), {
    
    child.setSpatialPosition(parent1.spatialPosition)
    return(T)
    
  })
 
)

There is also an experimental function in the package to convert SLiM code into slimr syntax (as_slimr_script()), but there are still some edge cases where it doesn't work. If you are interested in trying out the package, feel free. If you run into any problems just post an issue on the github page. I intend to add functions to automatically generate animation sometime soon. Cheers.

Also, hi Martin! I recently saw your twitter posts about slendr and have been meaning to reach out to you about it soon. I feel like our respective, independently developed packages have different enough goals to coexist, but perhaps we might consider combining our efforts in some way in the future?

Russell Dinnage



Reply all
Reply to author
Forward
0 new messages