Source code for helpers

################################################################################
'''Module "helpers.py", July 2010, is part of
BETR-Research by Harald von Waldow <hvwaldow@chem.ethz.ch>, which is
based on BETR-Global by Matt MacLeod <matthew.macleod@chem.ethz.ch>

This module contains various helper functions'''
################################################################################
from numpy import *
from globalz import *
import scipy.sparse as sp
import sys

def findemptycells(A):
    """ returns a list of indices with zero rows resp. columns"""
    B=A.toarray()
    idx=[]
    for i in range(0,B.shape[0]):
        if (all(B[i,:] == 0)):
            idx.append(i)
            if (not all(B[:,i] == 0)):
                sys.exit("findemptycells: empty cell receiving some ! Abort !")
    return(idx)

def compressmat(A, killidx):
    ''' deletes rows and columns in killidx from matrix'''
    spformat=A.getformat()
    B=sp.coo_matrix(delete(delete(A.todense(),killidx,0), killidx,1))
    return B.asformat(spformat)

def compressvec(v, killidx):
    """ Takes vector v and returns weeded vector"""
    vw = delete(v, killidx)
    return vw

def expandmatrix(A, killidx):
    spformat=A.getformat()
    '''re-expands previosly comprerssed matrix'''
    killidx=sort(killidx)
    B=A.toarray()
    for i in killidx:
        B = insert(B,i,[0],axis=0)
        B = insert(B,i,[0],axis=1)
    B = sp.coo_matrix(matrix(B))
    return(B.asformat(spformat))

def expandvec(v, killidx):
    """ Re - inserts zeros at the killidx - positions"""
    ve=copy(v)
    killidx=sort(killidx)
    for i in killidx:
        ve=insert(ve,i,0)
    return ve

[docs]def cell2regcomp(cell,m): ''' translates cell index into region-number and compartment ID ''' reg=floor(cell/m.nocomp) + 1 comp=mod(cell,m.nocomp) + 1 return (reg.astype(int),comp.astype(int))
[docs]def tocell(reg,comp,m): ''' translates region number and compartment ID into cell-index''' cell=m.nocomp*(reg-1)+comp-1 return cell ###############################################################################