Created
November 11, 2013 00:54
-
-
Save smoitra87/7406091 to your computer and use it in GitHub Desktop.
Generates Multifractal data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
# Copyright 2013 Subhodeep Moitra, all rights reserved | |
# [email protected] | |
import argparse | |
import os,sys | |
def log(n,base) : | |
return 0 if n==1 else 1+log(n/base,base) | |
def get_trace(args) : | |
""" Returns trace """ | |
base = len(args.biases) | |
depth = log(args.ticks[0],base) | |
if args.biases[0] > args.biases[1] : | |
args.biases.reverse() | |
probs = [b/100. for b in args.biases] | |
def cdf_sub(i,arr): | |
""" Convert pdf to cdf """ | |
if i < 0 : return 0 | |
arr[i] += cdf_sub(i-1,arr) | |
return arr[i] | |
convert_to_cdf = lambda arr : cdf_sub(len(arr)-1,arr) | |
convert_to_cdf(probs) | |
import random | |
def sample(pos,level): | |
""" Recurse levels and get a pos""" | |
if level == 0 : return pos | |
usample = random.random() | |
#binIdx = [ prob < usample for prob in probs].index(True) | |
binIdx = next((idx for idx,prob in enumerate(probs) | |
if usample<prob),None) | |
return sample(base*pos+binIdx,level-1) | |
return [sample(0,depth) for _ in range(args.accesses[0])] | |
if __name__ == '__main__' : | |
parser = argparse.ArgumentParser(description="Generate multifract code") | |
parser.add_argument('--biases',dest='biases',nargs='+',type=float, | |
help='The biases',default=[80,20]) | |
parser.add_argument('--ticks',dest='ticks',nargs=1,type=int, | |
help="Number of time ticks",default=[2048]) | |
parser.add_argument('--disk-accesses',dest='accesses',nargs=1,type=int, | |
help="Number of Disc Accesses",default=[2048]) | |
args = parser.parse_args() | |
trace = get_trace(args) | |
try : | |
fout = open('trace.dat','w') | |
for val in trace : | |
print >>fout, val | |
finally: | |
fout.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment