#NEWLINE# def test_dtypes( ) : #NEWLINE# #INDENT# batch_size = 2 #NEWLINE# dtype_is_none_msg = 'self.dtype is None, so you must provide a non-None dtype argument to this method.' #NEWLINE# all_scalar_dtypes = tuple ( ( t . dtype for t in theano . scalar . all_types ) ) #NEWLINE# def underspecifies_dtypes( from_space , to_dtype ) : #NEWLINE# #INDENT# '\n        Returns True iff the from_space and to_dtype are both None. If\n        from_space is a CompositeSpace, this recurses into its tree of\n        subspaces.\n        ' #NEWLINE# if isinstance ( from_space , CompositeSpace ) : #NEWLINE# #INDENT# if ( not isinstance ( to_dtype , tuple ) ) : #NEWLINE# #INDENT# return any ( ( underspecifies_dtypes ( s , to_dtype ) for s in from_space . components ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return any ( ( underspecifies_dtypes ( s , d ) for ( s , d ) in safe_zip ( from_space . components , to_dtype ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# assert ( not isinstance ( to_dtype , tuple ) ) , 'Tree structure mismatch between from_space and to_dtype.' #NEWLINE# return ( ( from_space . dtype is None ) and ( to_dtype is None ) ) #NEWLINE# #UNINDENT# #UNINDENT# def get_expected_batch_dtype( from_space , to_dtype ) : #NEWLINE# #INDENT# '\n        Returns the expected dtype of a batch returned from\n        from_space.f(batch, to_dtype), where f is one of the three batch\n        creation methods (get_origin_batch, make_theano_batch, and\n        make_shared_batch)\n        ' #NEWLINE# if ( to_dtype == 'floatX' ) : #NEWLINE# #INDENT# to_dtype = theano . config . floatX #NEWLINE# #UNINDENT# if isinstance ( from_space , CompositeSpace ) : #NEWLINE# #INDENT# if ( not isinstance ( to_dtype , tuple ) ) : #NEWLINE# #INDENT# to_dtype = ( ( to_dtype , ) * len ( from_space . components ) ) #NEWLINE# #UNINDENT# return tuple ( ( get_expected_batch_dtype ( subspace , subtype ) for ( subspace , subtype ) in safe_zip ( from_space . components , to_dtype ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# assert ( not ( ( from_space . dtype is None ) and ( to_dtype is None ) ) ) #NEWLINE# return ( from_space . dtype if ( to_dtype is None ) else to_dtype ) #NEWLINE# #UNINDENT# #UNINDENT# def get_batch_dtype( batch ) : #NEWLINE# #INDENT# '\n        Returns the dtype of a batch, as a string, or nested tuple of strings.\n        For simple batches such as ndarray, this returns str(batch.dtype).\n        For the None batches "used" by NullSpace, this returns a special string\n        "NullSpace dtype".\n        For composite batches, this returns (nested) tuples of dtypes.\n        ' #NEWLINE# if isinstance ( batch , tuple ) : #NEWLINE# #INDENT# return tuple ( ( get_batch_dtype ( b ) for b in batch ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( batch is None ) : #NEWLINE# #INDENT# return 'NullSpace dtype' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return batch . dtype #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# def test_get_origin_batch( from_space , to_type ) : #NEWLINE# #INDENT# if underspecifies_dtypes ( from_space , to_type ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# from_space . get_origin_batch ( batch_size , dtype = to_type ) #NEWLINE# #UNINDENT# except TypeError as ex : #NEWLINE# #INDENT# assert ( dtype_is_none_msg in str ( ex ) ) #NEWLINE# #UNINDENT# except Exception as unexpected_ex : #NEWLINE# #INDENT# print ( ( 'Expected an exception of type TypeError with message %s, got a %s instead with message %s.' % ( dtype_is_none_msg , type ( unexpected_ex ) , str ( unexpected_ex ) ) ) ) #NEWLINE# raise unexpected_ex #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# #UNINDENT# batch = from_space . get_origin_batch ( batch_size , dtype = to_type ) #NEWLINE# assert ( get_batch_dtype ( batch ) == get_expected_batch_dtype ( from_space , to_type ) ) #NEWLINE# #UNINDENT# def test_make_shared_batch( from_space , to_type ) : #NEWLINE# #INDENT# if underspecifies_dtypes ( from_space , to_type ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# from_space . make_shared_batch ( batch_size , dtype = to_type ) #NEWLINE# #UNINDENT# except TypeError as ex : #NEWLINE# #INDENT# assert ( dtype_is_none_msg in str ( ex ) ) #NEWLINE# #UNINDENT# except Exception as unexpected_ex : #NEWLINE# #INDENT# print ( ( 'Expected an exception of type TypeError with message %s, got a %s instead with message %s.' % ( dtype_is_none_msg , type ( unexpected_ex ) , str ( unexpected_ex ) ) ) ) #NEWLINE# raise unexpected_ex #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# #UNINDENT# batch = from_space . make_shared_batch ( batch_size = batch_size , name = 'batch' , dtype = to_type ) #NEWLINE# assert ( get_batch_dtype ( batch ) == get_expected_batch_dtype ( from_space , to_type ) ) , ( '\nget_batch_dtype(batch): %s\nget_expected_batch_dtype(from_space, to_type): %s' % ( get_batch_dtype ( batch ) , get_expected_batch_dtype ( from_space , to_type ) ) ) #NEWLINE# #UNINDENT# def test_make_theano_batch( from_space , to_type ) : #NEWLINE# #INDENT# kwargs = { #NEWLINE# #INDENT# 'name' : 'batch' , #NEWLINE# 'dtype' : to_type , #NEWLINE# #UNINDENT# } #NEWLINE# if ( not ( isinstance ( from_space , VectorSpace ) and from_space . sparse ) ) : #NEWLINE# #INDENT# kwargs [ 'batch_size' ] = batch_size #NEWLINE# #UNINDENT# if underspecifies_dtypes ( from_space , to_type ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# from_space . make_theano_batch ( ** kwargs ) #NEWLINE# #UNINDENT# except TypeError as ex : #NEWLINE# #INDENT# assert ( dtype_is_none_msg in str ( ex ) ) #NEWLINE# #UNINDENT# except Exception as unexpected_ex : #NEWLINE# #INDENT# print ( ( 'Expected an exception of type TypeError with message %s, got a %s instead with message %s.' % ( dtype_is_none_msg , type ( unexpected_ex ) , str ( unexpected_ex ) ) ) ) #NEWLINE# raise unexpected_ex #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# #UNINDENT# batch = from_space . make_theano_batch ( ** kwargs ) #NEWLINE# assert ( get_batch_dtype ( batch ) == get_expected_batch_dtype ( from_space , to_type ) ) #NEWLINE# #UNINDENT# def test_format( from_space , to_space , using_numeric_batch ) : #NEWLINE# #INDENT# '\n        Unit test for a call to from_space.np_format_as(batch, to_space)\n        ' #NEWLINE# for ( space , name ) in zip ( ( from_space , to_space ) , ( 'from_space' , 'to_space' ) ) : #NEWLINE# #INDENT# if ( not isinstance ( space , ( VectorSpace , Conv2DSpace , CompositeSpace ) ) ) : #NEWLINE# #INDENT# raise TypeError ( ( "This test only supports spaces of type VectorSpace, Conv2DSpace, and CompositeSpace, not %s's type %s" % ( name , type ( space ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# def get_batch( space , using_numeric_batch ) : #NEWLINE# #INDENT# "\n            Uses space.get_origin_batch() to return a numeric batch,\n            or space.get_theano_batch() to return a symbolic\n            Uses a fallback dtype if the space itself doesn't have one.\n            " #NEWLINE# def specifies_all_dtypes( space ) : #NEWLINE# #INDENT# '\n                Returns True iff space has a completely specified dtype.\n                ' #NEWLINE# if isinstance ( space , CompositeSpace ) : #NEWLINE# #INDENT# return all ( ( specifies_all_dtypes ( subspace ) for subspace in space . components ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( space . dtype is not None ) #NEWLINE# #UNINDENT# #UNINDENT# def replace_none_dtypes( dtype , fallback_dtype ) : #NEWLINE# #INDENT# '\n                Returns dtype, with any Nones replaced by fallback_dtype.\n                ' #NEWLINE# if isinstance ( dtype , tuple ) : #NEWLINE# #INDENT# return tuple ( ( replace_none_dtypes ( d , fallback_dtype ) for d in dtype ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( fallback_dtype if ( dtype is None ) else dtype ) #NEWLINE# #UNINDENT# #UNINDENT# kwargs = { #NEWLINE# #INDENT# 'batch_size' : batch_size , #NEWLINE# #UNINDENT# } #NEWLINE# fallback_dtype = theano . config . floatX #NEWLINE# if ( not specifies_all_dtypes ( space ) ) : #NEWLINE# #INDENT# kwargs [ 'dtype' ] = replace_none_dtypes ( space . dtype , fallback_dtype ) #NEWLINE# #UNINDENT# if using_numeric_batch : #NEWLINE# #INDENT# return space . get_origin_batch ( ** kwargs ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( isinstance ( space , VectorSpace ) and space . sparse ) : #NEWLINE# #INDENT# del kwargs [ 'batch_size' ] #NEWLINE# #UNINDENT# kwargs [ 'name' ] = 'space-generated batch' #NEWLINE# return space . make_theano_batch ( ** kwargs ) #NEWLINE# #UNINDENT# #UNINDENT# def get_expected_warning( from_space , from_batch , to_space ) : #NEWLINE# #INDENT# if ( isinstance ( from_space , CompositeSpace ) and isinstance ( to_space , CompositeSpace ) ) : #NEWLINE# #INDENT# for ( fs , fb , ts ) in safe_zip ( from_space . components , from_batch , to_space . components ) : #NEWLINE# #INDENT# ( warning , message ) = get_expected_warning ( fs , fb , ts ) #NEWLINE# if ( warning is not None ) : #NEWLINE# #INDENT# return ( warning , message ) #NEWLINE# #UNINDENT# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# if isinstance ( from_space , CompositeSpace ) : #NEWLINE# #INDENT# for ( fs , fb ) in safe_zip ( from_space . components , from_batch ) : #NEWLINE# #INDENT# ( warning , message ) = get_expected_warning ( fs , fb , to_space ) #NEWLINE# if ( warning is not None ) : #NEWLINE# #INDENT# return ( warning , message ) #NEWLINE# #UNINDENT# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# if isinstance ( to_space , CompositeSpace ) : #NEWLINE# #INDENT# if ( isinstance ( from_space , VectorSpace ) and isinstance ( from_batch , theano . sparse . SparseVariable ) ) : #NEWLINE# #INDENT# assert from_space . sparse #NEWLINE# return ( UserWarning , 'Formatting from a sparse VectorSpace to a CompositeSpace is currently (2 Jan 2014) a non-differentiable action. This is because it calls slicing operations on a sparse batch (e.g. "my_matrix[r:R, c:C]", which Theano does not yet have a gradient operator for. If autodifferentiation is reporting an error, this may be why.' ) #NEWLINE# #UNINDENT# for ts in to_space . components : #NEWLINE# #INDENT# ( warning , message ) = get_expected_warning ( from_space , from_batch , ts ) #NEWLINE# if ( warning is not None ) : #NEWLINE# #INDENT# return ( warning , message ) #NEWLINE# #UNINDENT# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# def get_expected_error( from_space , from_batch , to_space ) : #NEWLINE# #INDENT# '\n            Returns the type of error to be expected when calling\n            from_space.np_format_as(batch, to_space). Returns None if no error\n            should be expected.\n            ' #NEWLINE# def contains_different_dtypes( space ) : #NEWLINE# #INDENT# '\n                Returns true if space contains different dtypes. None is\n                considered distinct from all actual dtypes.\n                ' #NEWLINE# assert isinstance ( space , CompositeSpace ) #NEWLINE# def get_shared_dtype_if_any( space ) : #NEWLINE# #INDENT# "\n                    Returns space's dtype. If space is composite, returns the\n                    dtype used by all of its subcomponents. Returns False if\n                    the subcomponents use different dtypes.\n                    " #NEWLINE# if isinstance ( space , CompositeSpace ) : #NEWLINE# #INDENT# dtypes = tuple ( ( get_shared_dtype_if_any ( c ) for c in space . components ) ) #NEWLINE# assert ( len ( dtypes ) > 0 ) #NEWLINE# if any ( ( ( d != dtypes [ 0 ] ) for d in dtypes [ 1 : ] ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# return dtypes [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return space . dtype #NEWLINE# #UNINDENT# #UNINDENT# return ( get_shared_dtype_if_any ( space ) is False ) #NEWLINE# #UNINDENT# assert ( isinstance ( from_space , CompositeSpace ) == isinstance ( from_batch , tuple ) ) #NEWLINE# if ( isinstance ( from_space , CompositeSpace ) and isinstance ( to_space , CompositeSpace ) ) : #NEWLINE# #INDENT# for ( fs , fb , ts ) in safe_zip ( from_space . components , from_batch , to_space . components ) : #NEWLINE# #INDENT# ( error , message ) = get_expected_error ( fs , fb , ts ) #NEWLINE# if ( error is not None ) : #NEWLINE# #INDENT# return ( error , message ) #NEWLINE# #UNINDENT# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# if isinstance ( from_space , CompositeSpace ) : #NEWLINE# #INDENT# if isinstance ( to_space , Conv2DSpace ) : #NEWLINE# #INDENT# return ( NotImplementedError , 'CompositeSpace does not know how to format as Conv2DSpace' ) #NEWLINE# #UNINDENT# for ( fs , fb ) in safe_zip ( from_space . components , from_batch ) : #NEWLINE# #INDENT# ( error , message ) = get_expected_error ( fs , fb , to_space ) #NEWLINE# if ( error is not None ) : #NEWLINE# #INDENT# return ( error , message ) #NEWLINE# #UNINDENT# #UNINDENT# if ( isinstance ( to_space , VectorSpace ) and contains_different_dtypes ( from_space ) and ( to_space . dtype is None ) ) : #NEWLINE# #INDENT# return ( TypeError , 'Tried to format components with differing dtypes into a VectorSpace with no dtype of its own. dtypes: ' ) #NEWLINE# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# if isinstance ( to_space , CompositeSpace ) : #NEWLINE# #INDENT# if ( isinstance ( from_space , VectorSpace ) and isinstance ( from_batch , theano . sparse . SparseVariable ) ) : #NEWLINE# #INDENT# assert from_space . sparse #NEWLINE# return ( UserWarning , 'Formatting from a sparse VectorSpace to a CompositeSpace is currently (2 Jan 2014) a non-differentiable action. This is because it calls slicing operations on a sparse batch (e.g. "my_matrix[r:R, c:C]", which Theano does not yet have a gradient operator for. If autodifferentiation is reporting an error, this may be why.' ) #NEWLINE# #UNINDENT# if isinstance ( from_space , Conv2DSpace ) : #NEWLINE# #INDENT# return ( NotImplementedError , 'Conv2DSpace does not know how to format as CompositeSpace' ) #NEWLINE# #UNINDENT# for ts in to_space . components : #NEWLINE# #INDENT# ( error , message ) = get_expected_error ( from_space , from_batch , ts ) #NEWLINE# if ( error is not None ) : #NEWLINE# #INDENT# return ( error , message ) #NEWLINE# #UNINDENT# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# def is_sparse( space ) : #NEWLINE# #INDENT# return ( isinstance ( space , VectorSpace ) and space . sparse ) #NEWLINE# #UNINDENT# def is_complex( arg ) : #NEWLINE# #INDENT# '\n                Returns whether a space or a batch has a complex dtype.\n                ' #NEWLINE# return ( ( arg . dtype is not None ) and str ( arg . dtype ) . startswith ( 'complex' ) ) #NEWLINE# #UNINDENT# if isinstance ( from_batch , tuple ) : #NEWLINE# #INDENT# return ( TypeError , 'This space only supports simple dtypes, but received a composite batch.' ) #NEWLINE# #UNINDENT# if ( is_complex ( from_batch ) and ( not is_complex ( from_space ) ) ) : #NEWLINE# #INDENT# return ( TypeError , ( 'This space has a non-complex dtype (%s), and thus cannot support complex batches of type %s.' % ( from_space . dtype , from_batch . dtype ) ) ) #NEWLINE# #UNINDENT# if ( ( from_space . dtype is not None ) and ( from_space . dtype != from_batch . dtype ) ) : #NEWLINE# #INDENT# return ( TypeError , ( 'This space is for dtype %s, but recieved a batch of dtype %s.' % ( from_space . dtype , from_batch . dtype ) ) ) #NEWLINE# #UNINDENT# if ( is_sparse ( from_space ) and isinstance ( to_space , Conv2DSpace ) ) : #NEWLINE# #INDENT# return ( TypeError , 'Formatting a SparseVariable to a Conv2DSpace is not supported, since neither scipy nor Theano has sparse tensors with more than 2 dimensions. We need 4 dimensions to represent a Conv2DSpace batch' ) #NEWLINE# #UNINDENT# if ( is_complex ( from_space ) and ( not is_complex ( to_space ) ) ) : #NEWLINE# #INDENT# if is_symbolic_batch ( from_batch ) : #NEWLINE# #INDENT# return ( TypeError , 'Casting from complex to real is ambiguous' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( np . ComplexWarning , 'Casting complex values to real discards the imaginary part' ) #NEWLINE# #UNINDENT# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# def get_expected_formatted_dtype( from_batch , to_space ) : #NEWLINE# #INDENT# '\n            Returns the expected dtype of the batch returned from a call to\n            from_batch.format_as(batch, to_space). If the returned batch is a\n            nested tuple, the expected dtype will also a nested tuple.\n            ' #NEWLINE# def get_single_dtype( batch ) : #NEWLINE# #INDENT# '\n                Returns the dtype shared by all leaf nodes of the nested batch.\n                If the nested batch contains differing dtypes, this throws an\n                AssertionError. None counts as a different dtype than non-None.\n                ' #NEWLINE# if isinstance ( batch , tuple ) : #NEWLINE# #INDENT# assert ( len ( batch ) > 0 ) #NEWLINE# child_dtypes = tuple ( ( get_single_dtype ( b ) for b in batch ) ) #NEWLINE# if any ( ( ( c != child_dtypes [ 0 ] ) for c in child_dtypes [ 1 : ] ) ) : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# return child_dtypes [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return batch . dtype #NEWLINE# #UNINDENT# #UNINDENT# if ( isinstance ( from_batch , tuple ) and isinstance ( to_space , CompositeSpace ) ) : #NEWLINE# #INDENT# return tuple ( ( get_expected_formatted_dtype ( b , s ) for ( b , s ) in safe_zip ( from_batch , to_space . components ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( from_batch , tuple ) : #NEWLINE# #INDENT# if ( to_space . dtype is not None ) : #NEWLINE# #INDENT# return to_space . dtype #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# result = get_batch_dtype ( from_batch ) #NEWLINE# if ( result is False ) : #NEWLINE# #INDENT# raise TypeError ( ( "From_batch doesn't have a single dtype: %s" % str ( get_batch_dtype ( from_batch ) ) ) ) #NEWLINE# #UNINDENT# return result #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( to_space , CompositeSpace ) : #NEWLINE# #INDENT# return tuple ( ( get_expected_formatted_dtype ( from_batch , s ) for s in to_space . components ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( to_space . dtype is None ) : #NEWLINE# #INDENT# assert ( from_batch . dtype is not None ) #NEWLINE# return str ( from_batch . dtype ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return to_space . dtype #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# from_batch = get_batch ( from_space , using_numeric_batch ) #NEWLINE# ( expected_error , expected_error_msg ) = get_expected_error ( from_space , from_batch , to_space ) #NEWLINE# if ( expected_error is not None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# with warnings . catch_warnings ( ) : #NEWLINE# #INDENT# warnings . simplefilter ( 'error' ) #NEWLINE# from_space . _format_as ( using_numeric_batch , from_batch , to_space ) #NEWLINE# #UNINDENT# #UNINDENT# except expected_error as ex : #NEWLINE# #INDENT# assert ( str ( ex ) . find ( expected_error_msg ) >= 0 ) #NEWLINE# #UNINDENT# except Exception as unknown_ex : #NEWLINE# #INDENT# print ( ( 'Expected exception of type %s, got %s.' % ( expected_error . __name__ , type ( unknown_ex ) ) ) ) #NEWLINE# raise unknown_ex #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# #UNINDENT# to_batch = from_space . _format_as ( using_numeric_batch , from_batch , to_space ) #NEWLINE# expected_dtypes = get_expected_formatted_dtype ( from_batch , to_space ) #NEWLINE# actual_dtypes = get_batch_dtype ( to_batch ) #NEWLINE# assert ( expected_dtypes == actual_dtypes ) , ( "\nexpected_dtypes: %s,\nactual_dtypes: %s \nfrom_space: %s\nfrom_batch's dtype: %s\nfrom_batch is theano?: %s\nto_space: %s" % ( expected_dtypes , actual_dtypes , from_space , get_batch_dtype ( from_batch ) , is_symbolic_batch ( from_batch ) , to_space ) ) #NEWLINE# #UNINDENT# def test_dtype_getter( space ) : #NEWLINE# #INDENT# "\n        Tests the getter method of space's dtype property.\n        " #NEWLINE# def assert_composite_dtype_eq( space , dtype ) : #NEWLINE# #INDENT# "\n            Asserts that dtype is a nested tuple with exactly the same tree\n            structure as space, and that the dtypes of space's components and\n            their corresponding elements in <dtype> are equal.\n            " #NEWLINE# assert ( isinstance ( space , CompositeSpace ) == isinstance ( dtype , tuple ) ) #NEWLINE# if isinstance ( space , CompositeSpace ) : #NEWLINE# #INDENT# for ( s , d ) in safe_zip ( space . components , dtype ) : #NEWLINE# #INDENT# assert_composite_dtype_eq ( s , d ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# assert ( space . dtype == dtype ) #NEWLINE# #UNINDENT# #UNINDENT# if isinstance ( space , SimplyTypedSpace ) : #NEWLINE# #INDENT# assert ( space . dtype == space . _dtype ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( space , NullSpace ) : #NEWLINE# #INDENT# assert ( space . dtype == "NullSpace's dtype" ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( space , CompositeSpace ) : #NEWLINE# #INDENT# assert_composite_dtype_eq ( space , space . dtype ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# def test_dtype_setter( space , dtype ) : #NEWLINE# #INDENT# "\n        Tests the setter method of space's dtype property.\n        " #NEWLINE# def get_expected_error( space , dtype ) : #NEWLINE# #INDENT# '\n            If calling space.dtype = dtype is expected to throw an exception,\n            this returns (exception_class, exception_message).\n\n            If no exception is to be expected, this returns (None, None).\n            ' #NEWLINE# if isinstance ( space , CompositeSpace ) : #NEWLINE# #INDENT# if isinstance ( dtype , tuple ) : #NEWLINE# #INDENT# if ( len ( space . components ) != len ( dtype ) ) : #NEWLINE# #INDENT# return ( ValueError , 'Argument 0 has length ' ) #NEWLINE# #UNINDENT# for ( s , d ) in safe_zip ( space . components , dtype ) : #NEWLINE# #INDENT# ( error , message ) = get_expected_error ( s , d ) #NEWLINE# if ( error is not None ) : #NEWLINE# #INDENT# return ( error , message ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for s in space . components : #NEWLINE# #INDENT# ( error , message ) = get_expected_error ( s , dtype ) #NEWLINE# if ( error is not None ) : #NEWLINE# #INDENT# return ( error , message ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# if isinstance ( space , SimplyTypedSpace ) : #NEWLINE# #INDENT# if ( not any ( ( ( dtype is None ) , ( dtype == 'floatX' ) , ( dtype in all_scalar_dtypes ) ) ) ) : #NEWLINE# #INDENT# return ( TypeError , ( 'Unrecognized value "%s" (type %s) for dtype arg' % ( dtype , type ( dtype ) ) ) ) #NEWLINE# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# if isinstance ( space , NullSpace ) : #NEWLINE# #INDENT# nullspace_dtype = NullSpace ( ) . dtype #NEWLINE# if ( dtype != nullspace_dtype ) : #NEWLINE# #INDENT# return ( TypeError , ( 'NullSpace can only take the bogus dtype "%s"' % nullspace_dtype ) ) #NEWLINE# #UNINDENT# return ( None , None ) #NEWLINE# #UNINDENT# raise NotImplementedError ( ( '%s not yet supported by this test' % type ( space ) ) ) #NEWLINE# #UNINDENT# def assert_dtype_equiv( space , dtype ) : #NEWLINE# #INDENT# '\n            Asserts that space.dtype and dtype are equivalent.\n            ' #NEWLINE# if isinstance ( space , CompositeSpace ) : #NEWLINE# #INDENT# if isinstance ( dtype , tuple ) : #NEWLINE# #INDENT# for ( s , d ) in safe_zip ( space . components , dtype ) : #NEWLINE# #INDENT# assert_dtype_equiv ( s , d ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for s in space . components : #NEWLINE# #INDENT# assert_dtype_equiv ( s , dtype ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# assert ( not isinstance ( dtype , tuple ) ) #NEWLINE# if ( dtype == 'floatX' ) : #NEWLINE# #INDENT# dtype = theano . config . floatX #NEWLINE# #UNINDENT# assert ( space . dtype == dtype ) , ( '%s not equal to %s' % ( space . dtype , dtype ) ) #NEWLINE# #UNINDENT# #UNINDENT# ( expected_error , expected_message ) = get_expected_error ( space , dtype ) #NEWLINE# if ( expected_error is not None ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# space . dtype = dtype #NEWLINE# #UNINDENT# except expected_error as ex : #NEWLINE# #INDENT# assert ( expected_message in str ( ex ) ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# print ( ( 'Expected exception of type %s, got %s instead.' % ( expected_error . __name__ , type ( ex ) ) ) ) #NEWLINE# raise ex #NEWLINE# #UNINDENT# return #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# space . dtype = dtype #NEWLINE# assert_dtype_equiv ( space , dtype ) #NEWLINE# #UNINDENT# #UNINDENT# def test_simply_typed_space_validate( space , batch_dtype , is_numeric ) : #NEWLINE# #INDENT# '\n        Creates a batch of batch_dtype, and sees if space validates it.\n        ' #NEWLINE# assert isinstance ( space , SimplyTypedSpace ) , ( '%s is not a SimplyTypedSpace' % type ( space ) ) #NEWLINE# batch_sizes = ( 1 , 3 ) #NEWLINE# if ( ( not is_numeric ) and isinstance ( space , VectorSpace ) and space . sparse ) : #NEWLINE# #INDENT# batch_sizes = ( None , ) #NEWLINE# #UNINDENT# for batch_size in batch_sizes : #NEWLINE# #INDENT# if is_numeric : #NEWLINE# #INDENT# batch = space . get_origin_batch ( dtype = batch_dtype , batch_size = batch_size ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# batch = space . make_theano_batch ( dtype = batch_dtype , batch_size = batch_size , name = 'test batch to validate' ) #NEWLINE# #UNINDENT# if ( ( space . dtype is not None ) and ( not np . can_cast ( batch . dtype , space . dtype ) ) ) : #NEWLINE# #INDENT# np . testing . assert_raises ( TypeError , space . _validate , ( is_numeric , batch ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# space . _validate ( is_numeric , batch ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# shape = np . array ( [ 2 , 3 , 4 ] , dtype = 'int' ) #NEWLINE# assert ( len ( shape ) == 3 ) #NEWLINE# dtypes = ( ( 'floatX' , None ) + all_scalar_dtypes ) #NEWLINE# vector_spaces = tuple ( ( VectorSpace ( dim = shape . prod ( ) , dtype = dt , sparse = s ) for dt in dtypes for s in ( True , False ) ) ) #NEWLINE# conv2d_spaces = tuple ( ( Conv2DSpace ( shape = shape [ : 2 ] , dtype = dt , num_channels = shape [ 2 ] ) for dt in dtypes ) ) #NEWLINE# n_dtypes = 2 #NEWLINE# old_nchannels = shape [ 2 ] #NEWLINE# shape [ 2 ] = ( old_nchannels / 2 ) #NEWLINE# assert ( ( shape [ 2 ] * 2 ) == old_nchannels ) , ( 'test code is broken: # of channels should start as an even number, not %d.' % old_nchannels ) #NEWLINE# def make_composite_space( dtype0 , dtype1 , use_conv2d ) : #NEWLINE# #INDENT# if use_conv2d : #NEWLINE# #INDENT# second_space = Conv2DSpace ( shape = shape [ : 2 ] , dtype = dtype1 , num_channels = shape [ 2 ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# second_space = VectorSpace ( dim = np . prod ( shape ) , dtype = dtype1 ) #NEWLINE# #UNINDENT# return CompositeSpace ( ( VectorSpace ( dim = shape . prod ( ) , dtype = dtype0 ) , second_space ) ) #NEWLINE# #UNINDENT# composite_spaces = tuple ( ( make_composite_space ( dtype0 , dtype1 , use_conv2d ) for ( dtype0 , dtype1 ) in zip ( dtypes [ : n_dtypes ] , dtypes [ ( - n_dtypes ) : ] ) for use_conv2d in [ True , False ] ) ) #NEWLINE# del n_dtypes #NEWLINE# composite_dtypes = ( ( None , 'int8' ) , ( 'complex128' , theano . config . floatX ) ) #NEWLINE# for from_space in composite_spaces : #NEWLINE# #INDENT# for to_dtype in composite_dtypes : #NEWLINE# #INDENT# test_get_origin_batch ( from_space , to_dtype ) #NEWLINE# test_make_shared_batch ( from_space , to_dtype ) #NEWLINE# test_make_theano_batch ( from_space , to_dtype ) #NEWLINE# test_dtype_setter ( from_space , to_dtype ) #NEWLINE# #UNINDENT# #UNINDENT# for is_numeric in ( True , False ) : #NEWLINE# #INDENT# for space in ( vector_spaces + conv2d_spaces ) : #NEWLINE# #INDENT# for batch_dtype in ( ( 'floatX' , ) + all_scalar_dtypes ) : #NEWLINE# #INDENT# if ( isinstance ( space , VectorSpace ) and space . sparse and ( batch_dtype in all_scalar_dtypes ) and ( batch_dtype not in theano . sparse . SparseType . dtype_set ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# test_simply_typed_space_validate ( space , batch_dtype , is_numeric ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# all_spaces = ( ( vector_spaces + conv2d_spaces ) + composite_spaces ) #NEWLINE# for from_space in all_spaces : #NEWLINE# #INDENT# test_dtype_getter ( from_space ) #NEWLINE# for to_dtype in dtypes : #NEWLINE# #INDENT# if ( isinstance ( from_space , VectorSpace ) and from_space . sparse and ( to_dtype in all_scalar_dtypes ) and ( to_dtype not in theano . sparse . SparseType . dtype_set ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# test_get_origin_batch ( from_space , to_dtype ) #NEWLINE# test_make_shared_batch ( from_space , to_dtype ) #NEWLINE# test_make_theano_batch ( from_space , to_dtype ) #NEWLINE# test_dtype_setter ( from_space , to_dtype ) #NEWLINE# #UNINDENT# for to_space in all_spaces : #NEWLINE# #INDENT# if ( isinstance ( to_space , VectorSpace ) and to_space . sparse and ( to_space . dtype in all_scalar_dtypes ) and ( to_space . dtype not in theano . sparse . SparseType . dtype_set ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# for is_numeric in ( True , False ) : #NEWLINE# #INDENT# test_format ( from_space , to_space , is_numeric )