Skip to content

Commit

Permalink
Merge pull request #33 from jmschrei/cleanups
Browse files Browse the repository at this point in the history
[MRG] ENH imports reduced
  • Loading branch information
Jacob Schreiber committed Sep 27, 2015
2 parents 3f40c06 + f933bb5 commit 3c869db
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 213 deletions.
59 changes: 7 additions & 52 deletions pomegranate/BayesianNetwork.pyx
@@ -1,59 +1,14 @@
# BayesianNetwork.pyx
# Contact: Jacob Schreiber ( jmschreiber91@gmail.com )

cimport cython
from cython.view cimport array as cvarray
from libc.math cimport log as clog, sqrt as csqrt, exp as cexp
import math, random, itertools as it, sys, bisect
import networkx

if sys.version_info[0] > 2:
# Set up for Python 3
from functools import reduce
xrange = range
izip = zip
else:
izip = it.izip

import numpy
cimport numpy

cimport utils
from utils cimport *

cimport distributions
from distributions cimport *

cimport base
from base cimport Model, State

from FactorGraph import *

# Define some useful constants
DEF NEGINF = float("-inf")
DEF INF = float("inf")
DEF SQRT_2_PI = 2.50662827463

# Useful python-based array-intended operations
def log(value):
"""
Return the natural log of the given value, or - infinity if the value is 0.
Can handle both scalar floats and numpy arrays.
"""

if isinstance( value, numpy.ndarray ):
to_return = numpy.zeros(( value.shape ))
to_return[ value > 0 ] = numpy.log( value[ value > 0 ] )
to_return[ value == 0 ] = NEGINF
return to_return
return _log( value )

def exp(value):
"""
Return e^value, or 0 if the value is - infinity.
"""

return numpy.exp(value)
from .base cimport Model
from .base cimport State
from .distributions cimport DiscreteDistribution
from .distributions cimport ConditionalProbabilityTable
from .FactorGraph import FactorGraph
from .utils cimport _log

cdef class BayesianNetwork( Model ):
"""
Expand Down Expand Up @@ -169,7 +124,7 @@ cdef class BayesianNetwork( Model ):
Another name for the train method.
"""

self.train( items, weights, intertia )
self.train( items, weights, inertia )


def train( self, items, weights=None, inertia=0.0 ):
Expand Down
56 changes: 8 additions & 48 deletions pomegranate/FactorGraph.pyx
@@ -1,57 +1,17 @@
# FactorGraph.pyx
# Contact: Jacob Schreiber ( jmschreiber91@gmail.com )

cimport cython
from cython.view cimport array as cvarray
from libc.math cimport log as clog, sqrt as csqrt, exp as cexp
import math, random, itertools as it, sys, bisect, operator
import networkx

if sys.version_info[0] > 2:
# Set up for Python 3
from functools import reduce
xrange = range
izip = zip
else:
izip = it.izip

import numpy
cimport numpy
import numpy
import sys

cimport utils
from utils cimport *

cimport distributions
from distributions cimport *

cimport base
from base cimport *

# Define some useful constants
DEF NEGINF = float("-inf")
DEF INF = float("inf")
DEF SQRT_2_PI = 2.50662827463

# Useful python-based array-intended operations
def log(value):
"""
Return the natural log of the given value, or - infinity if the value is 0.
Can handle both scalar floats and numpy arrays.
"""
from .base cimport Model
from .base cimport State
from .distributions cimport Distribution
from .distributions cimport MultivariateDistribution

if isinstance( value, numpy.ndarray ):
to_return = numpy.zeros(( value.shape ))
to_return[ value > 0 ] = numpy.log( value[ value > 0 ] )
to_return[ value == 0 ] = NEGINF
return to_return
return _log( value )

def exp(value):
"""
Return e^value, or 0 if the value is - infinity.
"""

return numpy.exp(value)
if sys.version_info[0] > 2:
xrange = range

cdef class FactorGraph( Model ):
"""
Expand Down
19 changes: 5 additions & 14 deletions pomegranate/base.pyx
@@ -1,31 +1,22 @@
# base.pyx
# Contact: Jacob Schreiber ( jmschreiber91@gmail.com )

cimport distributions
from distributions import Distribution
from .distributions cimport Distribution
from .utils cimport *

cimport utils
from utils cimport *

import numpy
cimport numpy

import networkx, sys
import itertools as it
import json
import numpy
import networkx
import sys

if sys.version_info[0] > 2:
# Set up for Python 3
from functools import reduce
xrange = range
izip = zip
else:
izip = it.izip

# Define some useful constants
DEF NEGINF = float("-inf")
DEF INF = float("inf")
DEF SQRT_2_PI = 2.50662827463

def log(value):
"""
Expand Down
36 changes: 18 additions & 18 deletions pomegranate/distributions.pyx
Expand Up @@ -4,37 +4,37 @@
# distributions.pyx
# Contact: Jacob Schreiber ( jmschreiber91@gmail.com )

cimport cython
from cython.view cimport array as cvarray
from libc.stdlib cimport calloc, free, realloc
from libc.string cimport memcpy, memset
from libc.math cimport log as clog, sqrt as csqrt, exp as cexp, lgamma, fabs
from libc.stdlib cimport calloc
from libc.stdlib cimport free
from libc.string cimport memset
from libc.math cimport exp as cexp
from libc.math cimport fabs
from libc.math cimport lgamma
from libc.math cimport sqrt as csqrt

import itertools as it
import json
import numpy
import random
import scipy.special
import sys

import math, random, itertools as it, sys, bisect, json
import networkx, time
from .utils cimport pair_lse
from .utils cimport _log

if sys.version_info[0] > 2:
# Set up for Python 3
from functools import reduce
xrange = range
izip = zip
else:
izip = it.izip

import numpy
cimport numpy

cimport utils
from utils cimport *

# Define some useful constants
DEF NEGINF = float("-inf")
DEF INF = float("inf")
DEF SQRT_2_PI = 2.50662827463
DEF LOG_2_PI = 1.83787706641

import scipy.special

def log(value):
"""
Return the natural log of the given value, or - infinity if the value is 0.
Expand Down Expand Up @@ -992,7 +992,7 @@ cdef class GammaDistribution( Distribution ):
# obvious.)
summaries = numpy.array( self.summaries )

statistic = math.log( numpy.average( summaries[:,0],
statistic = _log( numpy.average( summaries[:,0],
weights=summaries[:,3] ) ) - \
numpy.average( summaries[:,1],
weights=summaries[:,3] )
Expand Down Expand Up @@ -1101,7 +1101,7 @@ cdef class DiscreteDistribution( Distribution ):

for key in self.keys():
p[key] -= total
p[key] = math.exp( p[key] )
p[key] = cexp( p[key] )

return DiscreteDistribution( p )

Expand Down
41 changes: 13 additions & 28 deletions pomegranate/fsm.pyx
@@ -1,34 +1,19 @@
# bayesnet.pyx
# Contact: Jacob Schreiber ( jmschreiber91@gmail.com )

cimport cython
from cython.view cimport array as cvarray
from libc.math cimport log as clog, sqrt as csqrt, exp as cexp
import math, random, itertools as it, sys, bisect, json
import networkx

from libc.stdlib cimport calloc, free, realloc
from libc.string cimport memcpy, memset

if sys.version_info[0] > 2:
# Set up for Python 3
from functools import reduce
xrange = range
izip = zip
else:
izip = it.izip
from libc.stdlib cimport calloc
from libc.stdlib cimport free
from libc.string cimport memset

import json
import numpy
cimport numpy
import sys

cimport utils
from utils cimport *
from .base cimport Model
from .base cimport State

cimport distributions
from distributions cimport *

cimport base
from base cimport *
if sys.version_info[0] > 2:
from functools import reduce

cdef class FiniteStateMachine( Model ):
'''
Expand All @@ -41,7 +26,7 @@ cdef class FiniteStateMachine( Model ):
cdef object [:] edge_keys
cdef dict indices

cdef SIZE_t* out_edge_count
cdef int* out_edge_count
cdef int [:] out_transitions

def __init__( self, name=None, start=None ):
Expand Down Expand Up @@ -105,8 +90,8 @@ cdef class FiniteStateMachine( Model ):
# This holds numpy array indexed [a, b] to transition log probabilities
# from a to b, where a and b are state indices. It starts out saying all
# transitions are impossible.
self.out_edge_count = <SIZE_t*> calloc( n+1, sizeof(SIZE_t) )
memset( self.out_edge_count, 0, (n+1)*sizeof(SIZE_t) )
self.out_edge_count = <int*> calloc( n+1, sizeof(int) )
memset( self.out_edge_count, 0, (n+1)*sizeof(int) )

# Now we need to find a way of storing in-edges for a state in a manner
# that can be called in the cythonized methods below. This is basically
Expand Down Expand Up @@ -225,7 +210,7 @@ cdef class FiniteStateMachine( Model ):
'''

cdef int i, k, ki
cdef SIZE_t* out_edges = self.out_edge_count
cdef int* out_edges = self.out_edge_count


i = self.current_index
Expand Down
51 changes: 9 additions & 42 deletions pomegranate/gmm.pyx
@@ -1,57 +1,24 @@
# gmm.pyx
# Contact: Jacob Schreiber
# jmschr@cs.washington.edu
# Contact: Jacob Schreiber ( jmschreiber91@gmail.com )

cimport cython
from cython.view cimport array as cvarray
from libc.math cimport log as clog, exp as cexp
import itertools as it, sys, json
cimport numpy
import numpy
import json
import sys

if sys.version_info[0] > 2:
# Set up for Python 3
from functools import reduce
xrange = range
izip = zip
else:
izip = it.izip

import numpy
cimport numpy

cimport distributions
from distributions cimport *

cimport utils
from utils cimport *

cimport base
from base cimport *
from .distributions cimport Distribution
from .utils cimport _log
from .utils cimport pair_lse

# Define some useful constants
DEF NEGINF = float("-inf")
DEF INF = float("inf")
DEF SQRT_2_PI = 2.50662827463

# Useful python-based array-intended operations
def log(value):
"""
Return the natural log of the given value, or - infinity if the value is 0.
Can handle both scalar floats and numpy arrays.
"""

if isinstance( value, numpy.ndarray ):
to_return = numpy.zeros(( value.shape ))
to_return[ value > 0 ] = numpy.log( value[ value > 0 ] )
to_return[ value == 0 ] = NEGINF
return to_return
return _log( value )

def exp(value):
"""
Return e^value, or 0 if the value is - infinity.
"""

return numpy.exp(value)

def log_probability( model, samples ):
'''
Expand Down Expand Up @@ -105,7 +72,7 @@ cdef class GeneralMixtureModel:

for i in xrange( n ):
d = self.distributions[i]
log_probability = d.log_probability( point ) + clog( self.weights[i] )
log_probability = d.log_probability( point ) + _log( self.weights[i] )
log_probability_sum = pair_lse( log_probability_sum,
log_probability )

Expand Down

0 comments on commit 3c869db

Please sign in to comment.