<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> def ssq_error ( correct , estimate , mask ) : <EOL> """<STR_LIT>""" <EOL> assert correct . ndim == <NUM_LIT:2> <EOL> if np . sum ( estimate ** <NUM_LIT:2> * mask ) > <NUM_LIT> : <EOL> alpha = np . sum ( correct * estimate * mask ) / np . sum ( estimate ** <NUM_LIT:2> * mask ) <EOL> else : <EOL> alpha = <NUM_LIT:0.> <EOL> return np . sum ( mask * ( correct - alpha * estimate ) ** <NUM_LIT:2> ) <EOL> def local_error ( correct , estimate , mask , window_size , window_shift ) : <EOL> """<STR_LIT>""" <EOL> M , N = correct . shape [ : <NUM_LIT:2> ] <EOL> ssq = total = <NUM_LIT:0.> <EOL> for i in range ( <NUM_LIT:0> , M - window_size + <NUM_LIT:1> , window_shift ) : <EOL> for j in range ( <NUM_LIT:0> , N - window_size + <NUM_LIT:1> , window_shift ) : <EOL> correct_curr = correct [ i : i + window_size , j : j + window_size ] <EOL> estimate_curr = estimate [ i : i + window_size , j : j + window_size ] <EOL> mask_curr = mask [ i : i + window_size , j : j + window_size ] <EOL> ssq += ssq_error ( correct_curr , estimate_curr , mask_curr ) <EOL> total += np . sum ( mask_curr * correct_curr ** <NUM_LIT:2> ) <EOL> assert - np . isnan ( ssq / total ) <EOL> return ssq / total <EOL> def score_image ( true_shading , true_refl , estimate_shading , estimate_refl , mask , window_size = <NUM_LIT:20> ) : <EOL> return <NUM_LIT:0.5> * local_error ( true_shading , estimate_shading , mask , window_size , window_size // <NUM_LIT:2> ) + <NUM_LIT:0.5> * local_error ( true_refl , estimate_refl , mask , window_size , window_size // <NUM_LIT:2> ) </s>
