<s> import numpy as np <EOL> def growcut_python ( image , state , state_next , window_radius ) : <EOL> changes = <NUM_LIT:0> <EOL> height = image . shape [ <NUM_LIT:0> ] <EOL> width = image . shape [ <NUM_LIT:1> ] <EOL> for j in xrange ( width ) : <EOL> for i in xrange ( height ) : <EOL> winning_colony = state [ i , j , <NUM_LIT:0> ] <EOL> defense_strength = state [ i , j , <NUM_LIT:1> ] <EOL> for jj in xrange ( max ( j - window_radius , <NUM_LIT:0> ) , min ( j + window_radius + <NUM_LIT:1> , width ) ) : <EOL> for ii in xrange ( max ( i - window_radius , <NUM_LIT:0> ) , min ( i + window_radius + <NUM_LIT:1> , height ) ) : <EOL> if ii != i or jj != j : <EOL> d = image [ i , j , : ] - image [ ii , jj , : ] <EOL> s = np . sum ( d ** <NUM_LIT:2> ) <EOL> gval = <NUM_LIT:1.0> - np . sqrt ( s ) / np . sqrt ( <NUM_LIT:3> ) <EOL> attack_strength = gval * state [ ii , jj , <NUM_LIT:1> ] <EOL> if attack_strength > defense_strength : <EOL> defense_strength = attack_strength <EOL> winning_colony = state [ ii , jj , <NUM_LIT:0> ] <EOL> changes += <NUM_LIT:1> <EOL> state_next [ i , j , <NUM_LIT:0> ] = winning_colony <EOL> state_next [ i , j , <NUM_LIT:1> ] = defense_strength <EOL> return changes <EOL> from parakeet . testing_helpers import expect , run_local_tests <EOL> def run_growcut ( N = <NUM_LIT:3> , window_radius = <NUM_LIT:2> ) : <EOL> image = np . zeros ( ( N , N , <NUM_LIT:3> ) , dtype = np . double ) <EOL> state = np . zeros ( ( N , N , <NUM_LIT:2> ) , dtype = np . double ) <EOL> state_next = np . empty_like ( state ) <EOL> state [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> state [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> growcut_python ( image , state , state_next , window_radius ) <EOL> return state_next <EOL> def test_growcut ( ) : <EOL> res = run_growcut ( ) <EOL> expect ( run_growcut , [ ] , res ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> run_local_tests ( ) </s>
