<s> import numpy as np <EOL> from numpy . testing import assert_array_equal , assert_array_almost_equal <EOL> from pystruct . models import EdgeFeatureGraphCRF <EOL> from pystruct . learners import NSlackSSVM <EOL> from pystruct . datasets import generate_blocks_multinomial <EOL> from pystruct . utils import make_grid_edges <EOL> def edge_list_to_features ( edge_list ) : <EOL> edges = np . vstack ( edge_list ) <EOL> edge_features = np . zeros ( ( edges . shape [ <NUM_LIT:0> ] , <NUM_LIT:2> ) ) <EOL> edge_features [ : len ( edge_list [ <NUM_LIT:0> ] ) , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> edge_features [ len ( edge_list [ <NUM_LIT:0> ] ) : , <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> return edge_features <EOL> def test_multinomial_blocks_directional_simple ( ) : <EOL> X_ , Y_ = generate_blocks_multinomial ( n_samples = <NUM_LIT:10> , noise = <NUM_LIT> , seed = <NUM_LIT:0> ) <EOL> G = [ make_grid_edges ( x , return_lists = True ) for x in X_ ] <EOL> edge_features = [ edge_list_to_features ( edge_list ) for edge_list in G ] <EOL> edges = [ np . vstack ( g ) for g in G ] <EOL> X = list ( zip ( [ x . reshape ( - <NUM_LIT:1> , <NUM_LIT:3> ) for x in X_ ] , edges , edge_features ) ) <EOL> Y = [ y . ravel ( ) for y in Y_ ] <EOL> crf = EdgeFeatureGraphCRF ( n_states = <NUM_LIT:3> , n_edge_features = <NUM_LIT:2> ) <EOL> clf = NSlackSSVM ( model = crf , max_iter = <NUM_LIT:10> , C = <NUM_LIT:1> , check_constraints = False ) <EOL> clf . fit ( X , Y ) <EOL> Y_pred = clf . predict ( X ) <EOL> assert_array_equal ( Y , Y_pred ) <EOL> def test_multinomial_blocks_directional_anti_symmetric ( ) : <EOL> X_ , Y_ = generate_blocks_multinomial ( n_samples = <NUM_LIT:10> , noise = <NUM_LIT> , seed = <NUM_LIT:0> ) <EOL> G = [ make_grid_edges ( x , return_lists = True ) for x in X_ ] <EOL> edge_features = [ edge_list_to_features ( edge_list ) for edge_list in G ] <EOL> edges = [ np . vstack ( g ) for g in G ] <EOL> X = list ( zip ( [ x . reshape ( - <NUM_LIT:1> , <NUM_LIT:3> ) for x in X_ ] , edges , edge_features ) ) <EOL> Y = [ y . ravel ( ) for y in Y_ ] <EOL> crf = EdgeFeatureGraphCRF ( symmetric_edge_features = [ <NUM_LIT:0> ] , <EOL> antisymmetric_edge_features = [ <NUM_LIT:1> ] ) <EOL> clf = NSlackSSVM ( model = crf , C = <NUM_LIT:100> ) <EOL> clf . fit ( X , Y ) <EOL> Y_pred = clf . predict ( X ) <EOL> assert_array_equal ( Y , Y_pred ) <EOL> pairwise_params = clf . w [ - <NUM_LIT:9> * <NUM_LIT:2> : ] . reshape ( <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> sym = pairwise_params [ <NUM_LIT:0> ] <EOL> antisym = pairwise_params [ <NUM_LIT:1> ] <EOL> assert_array_almost_equal ( sym , sym . T ) <EOL> assert_array_almost_equal ( antisym , - antisym . T ) </s>
