Skip to content

Instantly share code, notes, and snippets.

View rhz's full-sized avatar

Ricardo Honorato-Zimmer rhz

View GitHub Profile
(require '[clojure.string :as s]
'[incanter.stats :as stats]
'[clojure.contrib.generic.math-functions :as m]
'[clojure.contrib.combinatorics :as c])
;;;; Conversion
(defn get-rows [s]
(let [[sep & ss] (.split s "\n")]
(take-nth 2 (partition-by #{sep} ss))))
(defprotocol AKappaExpression
"Kappa expression"
(agents [e] "get all agents in this expression")
(complexes [e] "get all complexes in this expression")
(subexpr [e ids] "get a subexpression for the given agent ids")
(merge [e1 e2] "merge two expressions")
(count-automorphisms [e] "count the number of automorphisms")
(expression? [e] "predicate for AKappaExpression")
(match [e1 e2] "returns a map from agent ids in e1 to their matching agent ids in e2")
;; should this methods be part of AKappaAgent?
(ns mca.gillespie
(:require [clojure.contrib.generic.math-functions :as math]))
(defn choice [m]
(let [r (rand (apply + (vals m)))]
(loop [[[k w] & kws] (seq m)
sum 0]
(if (or (nil? kws) (< r (+ sum w))) k
(recur kws (+ sum w))))))
(defn get-molecule [state mol]
(get-in state [:mixture mol] 0))
(defn get-activities [state] ;; FIXME this fn takes upto 80% of the time
(into {} (for [{:keys [lhs rate] :as r} (:rxns state)]
[r (apply * rate (for [[mol freq] lhs]
(* (get-molecule state mol) freq)))])))
import qualified Data.Map as Map
import Random (randomRIO)
type Mol = String
type Mixture = Map.Map Mol Integer
type Rxn = (Mixture, (Mixture -> Mixture)) -- (Lhs, Action)
type State = (Double, Integer, Mixture) -- (Time, NumSteps, Mixture)
getActivities :: [Rxn] -> Mixture -> [Integer]
getActivities rxns mixture = []
import qualified Data.Map as Map
type Mol = String
type Mixture = Map.Map Mol Integer
type Rxn = (Mixture, (Mixture -> Mixture), Double)
getActivities :: [Rxn] -> Mixture -> [Double]
getActivities rxns mixture =
let f acc mol sc = if sc == 1 -- sc is stoichiometric coefficient
then fromIntegral $ mixture Map.! mol
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.Maybe
import Data.List
import Random (randomRIO)
import qualified Data.Enumerator as E
import qualified Data.Enumerator.List as EL
type Mol = String
type Mixture = Map.Map Mol Integer
import qualified Data.Map as Map
import qualified Data.Set as Set
type IntMap = Map.Map Int Int
type IntSet = Set.Set Int
extend :: IntMap -> IntSet -> IntMap -> Maybe (IntMap, IntSet)
extend inj codom map = Map.foldrWithKey f (Just (inj, codom)) map
where f :: Int -> Int -> Maybe (IntMap, IntSet) -> Maybe (IntMap, IntSet)
f i j (Just (inj, codom)) = if Set.member j codom
@rhz
rhz / gist:2049084
Created March 16, 2012 08:15
Main.hs profiler result
JOB "Main 1.kappa 10000 0 500 +RTS -hc -T"
DATE "Fri Mar 16 08:13 2012"
SAMPLE_UNIT "seconds"
VALUE_UNIT "bytes"
BEGIN_SAMPLE 0.00
END_SAMPLE 0.00
BEGIN_SAMPLE 0.09
(2524)create/findNewInjs.cc... 104
(1740)evalMixture/evalObs.a... 192
(2308)diff.addModifications... 64
@rhz
rhz / gist:2049061
Created March 16, 2012 08:06
Main.hs profiler result
JOB "Main 1.kappa 10000 0 500 +RTS -hc -T"
DATE "Fri Mar 16 08:01 2012"
SAMPLE_UNIT "seconds"
VALUE_UNIT "bytes"
BEGIN_SAMPLE 0.00
END_SAMPLE 0.00
BEGIN_SAMPLE 0.09
(898)CAF:nodeOfId 16
(2523)findNewInjs.ccIdInjs/... 48
(1943)infinity/add.(...)/ad... 16