Just a warning, this code is ugly. I know there are better ways of doing this but this is just an exercise.
I am poking around with the functional programming side of python, but I keep encountering an error when I try and nest a number of function calls:
LEN = 4
def calcentropy(i):
entropy[i] = -1 * reduce(lambda x,y: x+y, map(lambda x: x*np.log2(x), map(lambda x: x * (float(1)/float(NUM)), map(count, range(0,LEN)))))
map(calcentropy, range(0,LEN))
I get an error message stating that I have a mismatch between types; float and None for the last call to range(): TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
When I do something like:
LEN = 4
def calcFreqs(i): do stuff to freqs
map(calcFreqs, range(0, LEN)
def calcentropy(i):
entropy[i] = -1 * reduce(lambda x,y: x+y, map(lambda x: x*np.log2(x), map(lambda x: x * (float(1)/float(NUM)), freqs))))
map(calcentropy, range(0,LEN))
I don't have any issues.
I think the problem is that LEN is no longer in scope of the call to range(). Is there a way I can fix this, or have I exceeded some sort of limit, and if so, what was it?
Sorry for not adding enough code, my mistake:
import numpy as np
LEN = 4
freqs = np.zeros(4 * LEN, dtype = np.float64)
sites = np.array([0,1,2,3,0,1,2,3,0,1,2,3], dtype = np.int8)
A = np.int8(0)
C = np.int8(1)
G = np.int8(2)
T = np.int8(3)
def count(i):
freqs[i * LEN + A] = E + reduce(lambda x,y:x+y, map(lambda x: 1 if x==A else 0, sites[i::LEN]))
freqs[i * LEN + C] = E + reduce(lambda x,y:x+y, map(lambda x: 1 if x==A else 0, sites[i::LEN]))
freqs[i * LEN + G] = E + reduce(lambda x,y:x+y, map(lambda x: 1 if x==A else 0, sites[i::LEN]))
freqs[i * LEN + T] = E + reduce(lambda x,y:x+y, map(lambda x: 1 if x==A else 0, sites[i::LEN]))
entropy = np.zeros(LEN, dtype = np.float64)
def calcentropy(i):
entropy[i] = -1 * reduce(lambda x,y: x+y, map(lambda x: x*np.log2(x), map(lambda x: x * (float(1)/float(NUM)), map(count, range(0,LEN)))))
map(calcentropy, range(0,LEN))
print entropy
info = map(lambda x: 2-x, entropy)