<s> import unittest <EOL> from nose . plugins . skip import SkipTest <EOL> import numpy <EOL> try : <EOL> import scipy . sparse as sp <EOL> except ImportError : <EOL> pass <EOL> import theano <EOL> from theano import config <EOL> from theano import tensor <EOL> from theano import sparse <EOL> if not theano . sparse . enable_sparse : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> from theano . sparse . sandbox . sp2 import ( <EOL> Poisson , poisson , Binomial , Multinomial , multinomial ) <EOL> from theano . tests import unittest_tools as utt <EOL> from theano . sparse . tests . test_basic import as_sparse_format <EOL> class PoissonTester ( utt . InferShapeTester ) : <EOL> x = { } <EOL> a = { } <EOL> for format in sparse . sparse_formats : <EOL> variable = getattr ( theano . sparse , format + '<STR_LIT>' ) <EOL> rand = numpy . array ( numpy . random . random_integers ( <NUM_LIT:3> , size = ( <NUM_LIT:3> , <NUM_LIT:4> ) ) - <NUM_LIT:1> , <EOL> dtype = theano . config . floatX ) <EOL> x [ format ] = variable ( ) <EOL> a [ format ] = as_sparse_format ( rand , format ) <EOL> def setUp ( self ) : <EOL> super ( PoissonTester , self ) . setUp ( ) <EOL> self . op_class = Poisson <EOL> def test_op ( self ) : <EOL> for format in sparse . sparse_formats : <EOL> f = theano . function ( <EOL> [ self . x [ format ] ] , <EOL> poisson ( self . x [ format ] ) ) <EOL> tested = f ( self . a [ format ] ) <EOL> assert tested . format == format <EOL> assert tested . dtype == self . a [ format ] . dtype <EOL> assert numpy . allclose ( numpy . floor ( tested . data ) , tested . data ) <EOL> assert tested . shape == self . a [ format ] . shape <EOL> def test_infer_shape ( self ) : <EOL> for format in sparse . sparse_formats : <EOL> self . _compile_and_check ( [ self . x [ format ] ] , <EOL> [ poisson ( self . x [ format ] ) ] , <EOL> [ self . a [ format ] ] , <EOL> self . op_class ) <EOL> class BinomialTester ( utt . InferShapeTester ) : <EOL> n = tensor . scalar ( ) <EOL> p = tensor . scalar ( ) <EOL> shape = tensor . lvector ( ) <EOL> _n = <NUM_LIT:5> <EOL> _p = <NUM_LIT> <EOL> _shape = numpy . asarray ( [ <NUM_LIT:3> , <NUM_LIT:5> ] , dtype = '<STR_LIT>' ) <EOL> inputs = [ n , p , shape ] <EOL> _inputs = [ _n , _p , _shape ] <EOL> def setUp ( self ) : <EOL> super ( BinomialTester , self ) . setUp ( ) <EOL> self . op_class = Binomial <EOL> def test_op ( self ) : <EOL> for sp_format in sparse . sparse_formats : <EOL> for o_type in sparse . float_dtypes : <EOL> f = theano . function ( <EOL> self . inputs , <EOL> Binomial ( sp_format , o_type ) ( * self . inputs ) ) <EOL> tested = f ( * self . _inputs ) <EOL> assert tested . shape == tuple ( self . _shape ) <EOL> assert tested . format == sp_format <EOL> assert tested . dtype == o_type <EOL> assert numpy . allclose ( numpy . floor ( tested . todense ( ) ) , <EOL> tested . todense ( ) ) <EOL> def test_infer_shape ( self ) : <EOL> for sp_format in sparse . sparse_formats : <EOL> for o_type in sparse . float_dtypes : <EOL> self . _compile_and_check ( <EOL> self . inputs , <EOL> [ Binomial ( sp_format , o_type ) ( * self . inputs ) ] , <EOL> self . _inputs , <EOL> self . op_class ) <EOL> class MultinomialTester ( utt . InferShapeTester ) : <EOL> p = sparse . csr_matrix ( ) <EOL> _p = sp . csr_matrix ( numpy . asarray ( [ [ <NUM_LIT:0.0> , <NUM_LIT:0.5> , <NUM_LIT:0.0> , <NUM_LIT:0.5> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.0> , <NUM_LIT:1.0> , <NUM_LIT:0.0> , <NUM_LIT:0.0> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.0> , <NUM_LIT> ] ] , <EOL> dtype = config . floatX ) ) <EOL> def setUp ( self ) : <EOL> super ( MultinomialTester , self ) . setUp ( ) <EOL> self . op_class = Multinomial <EOL> def test_op ( self ) : <EOL> n = tensor . lscalar ( ) <EOL> f = theano . function ( [ self . p , n ] , multinomial ( n , self . p ) ) <EOL> _n = <NUM_LIT:5> <EOL> tested = f ( self . _p , _n ) <EOL> assert tested . shape == self . _p . shape <EOL> assert numpy . allclose ( numpy . floor ( tested . todense ( ) ) , tested . todense ( ) ) <EOL> assert tested [ <NUM_LIT:2> , <NUM_LIT:1> ] == _n <EOL> n = tensor . lvector ( ) <EOL> f = theano . function ( [ self . p , n ] , multinomial ( n , self . p ) ) <EOL> _n = numpy . asarray ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] , dtype = '<STR_LIT>' ) <EOL> tested = f ( self . _p , _n ) <EOL> assert tested . shape == self . _p . shape <EOL> assert numpy . allclose ( numpy . floor ( tested . todense ( ) ) , tested . todense ( ) ) <EOL> assert tested [ <NUM_LIT:2> , <NUM_LIT:1> ] == _n [ <NUM_LIT:2> ] <EOL> def test_infer_shape ( self ) : <EOL> self . _compile_and_check ( [ self . p ] , <EOL> [ multinomial ( <NUM_LIT:5> , self . p ) ] , <EOL> [ self . _p ] , <EOL> self . op_class , <EOL> warn = False ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
