Code Monkey home page Code Monkey logo

fork's Introduction

Fork

Motivation

Fork is a collection of lightweight helper functions for multiverse analyses.

This software is released for educational purposes, not for active use. It is possible that questions and bug reports may go unanswered.

Existing packages for multiverse analyses such as multiverse on CRAN (Abhraneel Sarma, Matthew Kay) or the language-agnostic Boba (Yang Liu, Alex Kale, Tim Althoff, Jeffrey Heer) rely on a domain-specific language to specify branches, which allows multiverse branches to be specified without a bunch of if/elses that mess up the code flow. The downside to this approach is that they cannot be run as a plain R script or inside of an interactive session, and force you into a certain way of doing things. They also assume that every universe is independent of every other universe, so they miss opportunities to cache shared paths before they diverge. (Boba in particular does provide wonderful visualizations of the resulting multiverse, though!)

Fork, instead, is a sort of minimum viable approach to multiverse specifications. It provides a fork function to easily specify partial cross products of analytic choices, and an assortment of functions to help setting up a cache, ranking universes from most to least favored, and so on. Scenarios (universes) produced by repeated calls to fork are ordered to be maximally cacheable with a low memory footprint.

This lightweight approach does not do away with branching, but it minimizes their depth to one or two levels so that the resulting code still flows nicely and is easy to reason about.

In principe, multiverse analyses can be executed on multiple cores, but this requires a separate cache and a separate call to plan_scenarios for each core -- just partition the scenarios into two, three, four, etc. and then call the function on each part -- otherwise the cache hints provided by the plan will be wrong.

Installation

Usage

library('fork')

scenarios <- grid(add=1:3) |> 
  fork(add=1, multiply=5:10) |> 
  fork(add=2, multiply=10:15) |> 
  fork(add=3, multiply=15:20)

cache <- make_cache(scenarios)
planning <- plan_scenarios(scenarios)

analyze <- function(scenario) {
  invalidate_cache(cache, scenario$start_at)
  
  step <- step_through(scenario, cache)
  
  step('add', function(input) {
    10 + scenario$add
  })
  
  step('multiply', function(input) {
    input * scenario$multiply
  })
  
  return(cache$multiply)
}

multiverse <- planning |> 
  rowwise() |> 
  mutate(outcome = analyze(.data))

fork's People

Contributors

debrouwere avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.